无法将.mp4保存到android中的存储中

时间:2019-01-04 05:12:34

标签: android

我是android的新手,我想保存位于(原始)中的视频。我想将其保存到外部存储中。

这是我的代码。

String VIDEOpath = "android.resource://com.quad14.republicdayimggif/"+R.raw.video1;

private File getDisc(){

File file = Environment.  (Environment. DIRECTORY_DOWNLOADS);
        return new File(file,"VideoDemo");

    }



public void startSave(){

        File newfile;
    try {

        File currentFile = new File(VIDEOpath);

        File file=getDisc();
        if(!file.exists() && !file.mkdirs()){
            Snackbar.make(relativefLayoutvideo,"Soory can't create directory",Snackbar.LENGTH_SHORT).show();
            return;
        }
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyymmsshhmmss");
        String date=simpleDateFormat.format(new Date());
        String name="video"+date+".mp4";
        String file_name=file.getAbsolutePath()+"/"+name;


        newfile = new File(file_name);

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

            Snackbar.make(relativefLayoutvideo,"Video Save",Snackbar.LENGTH_SHORT).show();


        }else{
            Log.v("", "Video saving failed. Source file missing.");
        }



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

}

0 个答案:

没有答案