2012-09-29 50 views
0

我從服務器得到一個url &我想在我的android htc設備中保存視頻。 我收到一些錯誤:----- java.io.FileNotFoundException:/mnt/sdcard/543b8417-6e67-4755-8c68-b93373f9d9e2.wmv(權限被拒絕) &不支持地址系列。 我已經在清單文件中給予所有權限。我的代碼是: - videoUrl =「http://125.63.71.222/fliptest/uservideo/543b8417-6e67-4755-8c68-b93373f9d9e2.wmv」;如何從服務器中保存Android HTC中的視頻?

String LocalFilePath = videoUrl.substring(
      videoUrl.lastIndexOf("/") + 1, videoUrl.length()); 
    File dire = new File(android.os.Environment.getExternalStorageDirectory(), LocalFilePath); 






     int bytesread = 0; 
     byte[] bytes = new byte[8192]; 
     InputStream strm = null; 
     HttpResponse response = null; 
     HttpEntity entity = null; 
     String LocalFile = null; 
     FileOutputStream foStream; 
     BufferedOutputStream writer = null; 
     try { 
      DefaultHttpClient httpclient = new DefaultHttpClient(); 
      videoUrl = videoUrl.replace(" ", "%20"); 
      HttpGet httpget = new HttpGet(videoUrl); 

      response = httpclient.execute(httpget); 

      entity = response.getEntity(); 
      strm = entity.getContent(); 







      LocalFile = android.os.Environment.getExternalStorageDirectory() + LocalFilePath; 
      //directory = null; 
      foStream = new FileOutputStream(dire); 
      writer = new BufferedOutputStream(foStream); 
      do { 
       bytesread = strm.read(bytes, 0, bytes.length); 
       if (bytesread > 0) { 
        writer.write(bytes, 0, bytesread); 
        writer.flush(); 
       } 
      } while (bytesread > 0); 

      writer.close(); 
      foStream.close(); 

      strm.close(); 
      return; 

     } catch (Exception e) { 
     // dire.delete();    
      e.printStackTrace(); 

     } 
+0

你想要保存它?編寫路徑 – iTurki

+0

我想將它保存在我的Android設備gallary中。但是我沒有得到它的正確道路是什麼? –

回答

0

我想稍微修改代碼:

String videoUrl = "http://125.63.71.222/fliptest/uservideo/543b8417-6e67-4755-8c68-b93373f9d9e2.wmv"; 
String LocalFilePath = videoUrl.substring(videoUrl.lastIndexOf("/") + 1, videoUrl.length()); 
File dire = new File(android.os.Environment.getExternalStorageDirectory(), LocalFilePath); 

int bytesread = -1; // i would change it to -1 
byte[] bytes = new byte[8192]; 
InputStream strm = null; 
FileOutputStream foStream; 

try { 
    videoUrl = videoUrl.replace(" ", "%20"); 
    URL url = new URL(videoUrl); 
    URLConnection connection = url.openConnection(); 
    connection.connect(); 

    strm = new BufferedInputStream(url.openStream()); 
    foStream = new FileOutputStream(dire); 

    //typical pattern for reading 
    while ((bytesread = strm.read(bytes)) != -1) { 
     foStream.write(bytes, 0, bytesread); 
    } 

    foStream.flush(); 
    foStream.close(); 
    strm.close(); 

} catch (Exception e) { 
    ... 
} 

需要兩個權限來運行這段代碼 - 你已經提到你有他們,但快速提醒:

<uses-permission android:name="android.permission.INTERNET" /> 
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 

而且不要忘記異步運行這個任務。

最好的問候

+0

我仍然收到錯誤:-java.io.FileNotFoundException:/ sdcard/dcim/ –

+0

我給出了不同的路徑,如mnt/sdcard,系統/數據 –

+0

我測試了這個代碼我的HTC Sensation(與Android 4.0.3)並且工作得很好 - 我成功下載了你的視頻。你有哪個版本的機器人?哪個HTC?你在SD卡上有足夠的空間嗎?您可以使用任何文件查看器訪問您的應用程序之外的SD卡嗎?你能不能發佈完整的堆棧跟蹤?另請參閱「檢查介質可用性」一節中的代碼[鏈接](http://developer.android.com/guide/topics/data/data-storage.html)併發布結果。 – bart

相關問題