2011-12-19 29 views
0

我已將一個視頻文件下載到data/data/my_package/video.mp4,現在我想知道我可以如何播放此視頻。我正在使用Eclipse。提前致謝。如何從數據/數據/ my_package讀取數據

DownloadFromUrl("PATH"+videoname.mp4) 
{ 

public void DownloadFromUrl(String fileName) { 

      try { 
        URL url = new URL("http://www.bestbusinessplaces.com/animation/admin/Animation/132404046113240392391323340839intelligent_cartridge.mp4-muxed_0.mp4-muxed.mp4"); //you can write here any link 

        File file = new File(fileName); 

        long startTime = System.currentTimeMillis(); 

        tv.setText("Starting download......from " + url); 

        URLConnection ucon = url.openConnection(); 

        InputStream is = ucon.getInputStream(); 

        BufferedInputStream bis = new BufferedInputStream(is); 

        /* 

        * Read bytes to the Buffer until there is nothing more to read(-1). 

        */ 

        ByteArrayBuffer baf = new ByteArrayBuffer(50); 

        int current = 0; 

        while ((current = bis.read()) != -1) { 

          baf.append((byte) current); 

        } 


        FileOutputStream fos = new FileOutputStream(file); 

        fos.write(baf.toByteArray()); 

        fileOutput.close(); 
        fos.close(); 

        tv.setText("Download Completed in" + ((System.currentTimeMillis() - startTime)/1000) + " sec"); 

      } catch (IOException e) { 

       tv.setText("Error: " + e); 

      } 

    } 
+0

此視頻文件的權限是什麼? – user370305 2011-12-19 10:20:29

+0

-rw -------這是此視頻文件的許可 – 2011-12-19 10:24:25

+0

這不起作用OutputStream myOutput = openFileOutput(「Video_filename.mp4」,Context.MODE_WORLD_READABLE); – 2011-12-19 11:08:54

回答

0

當存儲的視頻在內部存儲保證其具有MODE_WORLD_READABLE權限,以便媒體播放器可以訪問它,

所以,存儲視頻類似,

OutputStream myOutput = openFileOutput("Video_filename.mp4", Context.MODE_WORLD_READABLE); 

,然後使用路徑設置視頻源..

FileInputStream fileInputStream = new FileInputStream("/data/data/com.mypackage/myfile"); 
mediaPlayer.setDataSource(fileInputStream.getFD()); 

編輯:

URL url;   
URLConnection ucon = null; 

url = new URL("http://www.bestbusinessplaces.com/animation/admin/Animation/132404046113240392391323340839intelligent_cartridge.mp4-muxed_0.mp4-muxed.mp4"); 
ucon = url.openConnection(); 
InputStream is = null; 
is = ucon.getInputStream(); 
OutputStream myOutput = openFileOutput("test_video.mp4", Context.MODE_WORLD_READABLE); 

try { 
    byte[] buffer = new byte[8192]; 
    int length; 
    while ((length = is.read(buffer)) > 0) { 
      myOutput.write(buffer); 
     } 

    //Close the streams 
    myOutput.flush(); 
    myOutput.close(); 
    is.close(); 

    } catch (IOException e) { 

     }