2015-09-26 187 views
1

我已經從對應於視頻文件Android圖庫將視頻保存到內部存儲

VideoView videoView1 = (VideoView)promptsView.findViewById(R.id.videoView09); 
      String SrcPath = "/storage/emulated/0/DCIM/Camera/20150824_210148.mp4"; 
      videoView1.setVideoPath(SrcPath); 
      videoView1.requestFocus(); 
      videoView1.start(); 

現在內的路徑起着視頻videoView,我需要以某種方式將視頻從這個videoView商店內部存儲我的應用程序私人。

我已經成功地爲照片做到這一點使用

public String saveImageToInternalStorage(Bitmap image, String imgRequestedName) { 
    ContextWrapper cw = new ContextWrapper(getActivity()); 
    File directory = cw.getDir("imageDir", Context.MODE_PRIVATE); 
    File mypath=new File(directory,imgRequestedName+".jpg"); 
    String loc = mypath.getAbsolutePath(); 
    FileOutputStream fos = null; 
    try { 

     fos = new FileOutputStream(mypath); 
     image.compress(Bitmap.CompressFormat.JPEG, 70, fos); 
     SharedPreferences pref = getActivity().getApplicationContext().getSharedPreferences("MyPref", Context.MODE_PRIVATE); 
     final SharedPreferences.Editor editor = pref.edit(); 
     editor.putInt("totalImageCount",(pref.getInt("totalImageCount",0))+1); 
     editor.commit(); 

     fos.close(); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 

    return mypath.getAbsolutePath(); 
} 

我怎樣才能做視頻的相同呢?

以及如何從內部存儲器讀取視頻?

回答

0

使用它的路徑內部存儲保存視頻:

ContextWrapper cw = new ContextWrapper(getActivity()); 
File directory = cw.getDir("vidDir", Context.MODE_PRIVATE); 
File mypath=new File(directory,vidRequestedName+".mp4"); 

      try { 
       FileOutputStream newFile = new FileOutputStream (mypath); 
       //path 0 = current path of the video 
       FileInputStream oldFile = new FileInputStream (path0); 

       // Transfer bytes from in to out 
       byte[] buf = new byte[1024]; 
       int len; 
       while ((len = oldFile.read(buf)) > 0) { 
        newFile.write(buf, 0, len); 
       } 
       newFile.flush(); 
       newFile.close(); 
       oldFile.close(); 
0

您應該使用uri。在示例中,我在資源中創建了一個「原始」文件,但您可以根據需要進行修改。

Uri uri = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.example.mp4); 
videoView.setVideoURI(uri);