gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 21:09:06 27 4
gpt4 key购买 nike

我目前使用 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();

}

}

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

最佳答案

以下是用户选择视频后如何将视频保存到内部存储的方法。

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




}

}

注意:不要忘记向 list 添加权限。

关于java - Android - 将视频保存到内部存储,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41606939/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com