0
A
回答
0
/** this function will download content from the internet */
static int writeData(String fileurl, boolean append, String path,
String filename, Activity mContext) throws CustomException {
URL myfileurl = null;
ByteArrayBuffer baf = null;
HttpURLConnection conn = null;
String mimeType="";
final int length;
try {
myfileurl = new URL(fileurl);
} catch (MalformedURLException e) {
e.printStackTrace();
}
try {
conn = (HttpURLConnection) myfileurl
.openConnection();
conn.setDoInput(true);
conn.connect();
conn.setConnectTimeout(100000);
length = conn.getContentLength();
mimeType=conn.getContentType().toString();
System.out.println("Extension..."+mimeType);
if(mimeType.equalsIgnoreCase("application/vnd.adobe.adept+xml") || mimeType.equalsIgnoreCase("text/html; charset=utf-8"))
return 0;
if (length > 0) {
InputStream is = conn.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
baf = new ByteArrayBuffer(1000);
int current = 0;
while ((current = bis.read()) != -1) {
try {
baf.append((byte) current);
mBufferError=false;
} catch (Exception e){
// TODO: handle exception
mBufferError=true;
e.printStackTrace();
throw new CustomException("@@@ memory problem ", "Buffer Error");
}
}
}
} catch (IOException e) {
mBufferError=true;
e.printStackTrace();
}
try{
if(conn.getResponseCode()==200 && mBufferError==false)
{
path = path + "/" + filename;
boolean appendData = append;
FileOutputStream foutstream;
File file = new File(path);
boolean exist = false;
try {
if (appendData)
exist = file.exists();
else
exist = file.createNewFile();
} catch (IOException e) {
try {
return 1;
} catch (Exception err) {
Log.e("SAX", err.toString());
}
}
if (!appendData && !exist) {
} else if (appendData && !exist) {
} else {
try {
foutstream = new FileOutputStream(file, appendData);
foutstream.write(baf.toByteArray());
foutstream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}catch (Exception e) {
// TODO: handle exception
throw new CustomException("@@@ I/O problem ", "I/O Error");
}
return 1;
}
下載一次完整的搜索帶擴展名(名爲.3gp)文件的視頻 希望它有助於
0
檢查這個環節,
https://stackoverflow.com/search?q=how+to+download+mp3+%2Cvideos+from+server+in+android
試試這個代碼
url = "your url name+filename.jpg,mp3,etc..."
FileName = "/sdcard/savefilename" // save in your sdcard
try{
java.io.BufferedInputStream in = new java.io.BufferedInputStream(new java.net.URL(url).openStream());
java.io.FileOutputStream fos = new java.io.FileOutputStream(FileName);
java.io.BufferedOutputStream bout = new BufferedOutputStream(fos,1024);
byte[] data = new byte[1024];
int x=0;
while((x=in.read(data,0,1024))>=0){
bout.write(data,0,x);
}
fos.flush();
bout.flush();
fos.close();
bout.close();
in.close();
}
catch (Exception ex)
{
}
後要使用的MediaPlayer 並在活動 創建媒體播放器的對象和發揮。 mp.reset(); mp.start();
相關問題
- 1. 從Web服務器接收數據
- 2. 如何使用ajax從服務器接收數據?
- 3. 如何從服務器接收返回的JSON數據?
- 4. 如何從服務器接收數據到Android
- 5. 如何從Android的接收數據到PHP的Web服務器
- 6. 如何從asp.net服務器連續接收數據到flash?
- 7. 如何在SSL連接的服務器端接收數據?
- 8. 直接從WebSocket服務器接收Leap數據到Unity3D
- 9. 從服務器收集迭代數據
- 10. 綁定驅動程序如何從服務器接口接收RX數據包
- 11. 如何通過iOS中的套接字從服務器接收數據?
- 12. 如何使用python套接字從廣播服務器接收大數據
- 13. 如何發送數據到php服務器和使用POST和httpurl連接在php服務器接收數據
- 14. 如何在Web服務器上接收GPS數據?
- 15. 如何接收服務器發送的所有數據?
- 16. 如何跟蹤Apache Web服務器接收到的HTTP數據?
- 17. 如何讓我的TCP服務器不斷接收數據?
- 18. 客戶端無法從服務器接收數據
- 19. 從服務器接收數據時出錯
- 20. 從Socket流服務器接收多個數據長度 - PYTHON3
- 21. UDP客戶端無法從服務器接收數據
- 22. 沒有從服務器接收數據使用改進2
- 23. Socket.io客戶端無法從服務器接收數據正確
- 24. 從服務器接收數據的最佳方式
- 25. UdpClient C#客戶端不會從服務器接收數據
- 26. 從android系統udp服務器接收udp數據?
- 27. 使用AJAX從另一臺服務器接收數據
- 28. 如何在從服務器接收數據時在TextView中創建標籤?
- 29. 如何接收和發送數據從服務器到客戶端循環
- 30. 從廣播接收器啓動服務