【问题标题】:Save video to Internal Storage将视频保存到内部存储
【发布时间】:2015-12-23 22:52:41
【问题描述】:

我有一个 videoView,它从对应于 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 中的视频私下存储到我的应用程序的内部存储中。

我已经设法使用

为照片做到这一点
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();
}

我怎样才能为视频做同样的事情?

我如何从内部存储中读取视频?

【问题讨论】:

    标签: android video file-io android-gallery internal-storage


    【解决方案1】:

    以下是将视频私下保存到应用内部存储的代码。 以及从内部存储中读取视频的代码。 希望这会有所帮助。

    //For saving Video...
    
    private void saveVideoToInternalStorage (String filePath) {
    
        File newfile;
    
        try {
    
            File currentFile = new File(filePath);
            String fileName = currentFile.getName();
    
            ContextWrapper cw = new ContextWrapper(getApplicationContext());
            File directory = cw.getDir("videoDir", Context.MODE_PRIVATE);
    
    
            newfile = new File(directory, fileName);
    
            if(currentFile.exists()){
    
                InputStream in = new FileInputStream(currentFile);
                OutputStream out = new FileOutputStream(newfile);
    
                // Copy the bits from instream to outstream
                byte[] buf = new byte[1024];
                int len;
    
                while ((len = in.read(buf)) > 0) {
                    out.write(buf, 0, len);
                }
    
                in.close();
                out.close();
    
                Log.v("", "Video file saved successfully.");
    
            }else{
                Log.v("", "Video saving failed. Source file missing.");
            }
    
    
    
        } catch (Exception e) {
            e.printStackTrace();
        }
    
    }
    
    private void loadVideoFromInternalStorage(String filePath){
    
        Uri uri = Uri.parse(Environment.getExternalStorageDirectory()+filePath);
        myVideoView.setVideoURI(uri);
    
    }
    

    【讨论】:

      【解决方案2】:

      使用视频的内部存储路径保存视频:

       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();
      

      【讨论】:

        【解决方案3】:

        您应该使用uri。在示例中,我在资源中创建了一个“原始”文件,但您可以根据需要进行修改。

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

        【讨论】:

          【解决方案4】:

          使用 uuid 将视频保存在内部存储中

          private void saveVideoToInternalStorage() {
              UUID uuid = UUID.randomUUID();
          
              try {
          
                  File currentFile = new File(filePath);
                  File loc = Environment.getExternalStorageDirectory();
                  File directory = new File(loc.getAbsolutePath()+"/FolderNameWhateverYouWant");
                  directory.mkdir();
                  String fileName = String.format( uuid+".mp4");
                  File newfile = new File(directory, fileName);
          
          
                  if(currentFile.exists()){
          
                      InputStream inputStream = new FileInputStream(currentFile);
                      OutputStream outputStream = new FileOutputStream(newfile);
          
                      byte[] buf = new byte[1024];
                      int len;
          
                      while ((len = inputStream.read(buf)) > 0) {
                          outputStream.write(buf, 0, len);
                      }
          
                      outputStream.flush();
                      inputStream.close();
                      outputStream.close();
          
                      Toast.makeText(getApplicationContext(),"Video has just saved!!",Toast.LENGTH_LONG).show();
          
                  }else{
                      Toast.makeText(getApplicationContext(),"Video has failed for saving!!",Toast.LENGTH_LONG).show();
                  }
          
          
              } catch (Exception e) {
                  e.printStackTrace();
              }
          
          }
          

          不要忘记请求权限

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

          【讨论】:

            猜你喜欢
            • 2020-06-30
            • 2023-03-08
            • 1970-01-01
            • 1970-01-01
            • 2018-02-20
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2016-01-04
            相关资源
            最近更新 更多