Android - 将视频保存到内部存储

时间:2017-01-12 06:58:03

标签: java android video storage

我目前使用FloatingActionButton打开图库并让用户选择视频,一旦选择了视频,它就会在videoView中打开。

public class Activity extends AppCompatActivity {
    private static final int pick = 100;
    Uri videoUri;
    VideoView videoview;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity);

        videoview = (VideoView) findViewById(R.id.videoview);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                openGallery();

            }


        });
    }

    private void openGallery() {
        Intent intent = new Intent(Intent.ACTION_PICK,     MediaStore.Video.Media.EXTERNAL_CONTENT_URI);
        startActivityForResult(intent, pick);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data)     {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK && requestCode == pick) {
            videoUri = data.getData();
            videoview.setVideoURI(videoUri);
            videoview.start();

        }

    }

我想在用户选择后将视频保存到内部存储空间?

2 个答案:

答案 0 :(得分:1)

在这里,用户选择视频后,可以将其保存到内部存储中。

在onActivityResult代码块中,添加以下内容:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)     {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK && requestCode == pick) {


        try {

            File newfile;

            AssetFileDescriptor videoAsset = getContentResolver().openAssetFileDescriptor(data.getData(), "r");
            FileInputStream in = videoAsset.createInputStream();

            File filepath = Environment.getExternalStorageDirectory();
            File dir = new File(filepath.getAbsolutePath() + "/" +"Your Folder Name" + "/");
            if (!dir.exists()) {
                dir.mkdirs();
            }

            newfile = new File(dir, "save_"+System.currentTimeMillis()+".mp4");

            if (newfile.exists()) newfile.delete();



            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("", "Copy file successful.");


            videoUri = data.getData();
            videoview.setVideoURI(videoUri);
            videoview.start();



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




    }

}

注意:别忘了向清单添加权限。

答案 1 :(得分:0)

private File saveInInternalStorage(Uri videoUri) {

   Bitmap finalBitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), videoUri);
    File file=null;
    FileOutputStream outputStream;
    try {
        Random generator = new Random();
        int n = 10000;
        n = generator.nextInt(n);
        file = new File(getCacheDir(), "MyCache"+n);

        FileOutputStream out = new FileOutputStream(file);
        finalBitmap.compress(Bitmap.CompressFormat.PNG, 90, out);
        out.flush();
        out.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return file;
}