【发布时间】:2017-01-13 07:07:56
【问题描述】:
我目前有一个应用程序,用户单击浮动操作按钮并选择一个视频文件,然后将该文件保存到不同的文件夹中。然后我想显示所有视频的缩略图。我看过一个使用 MediaStore 完成的教程系列,但是我无法设置 uri 的路径。
有人可以指点我正确的方向吗?
这是我的班级打开画廊并将视频保存到不同的路径:
public class Activity extends AppCompatActivity {
private static final int pick = 100;
Uri videoUri;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity);
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) {
try
{
Log.e("videopath","videopath");
AssetFileDescriptor videoAsset = getContentResolver().openAssetFileDescriptor(data.getData(), "r");
FileInputStream fis = videoAsset.createInputStream();
File root=new File(Environment.getExternalStorageDirectory(),"MYFOLDER");
if (!root.exists()) {
root.mkdirs();
}
File file;
file=new File(root,"android_"+System.currentTimeMillis()+".mp4" );
FileOutputStream fos = new FileOutputStream(file);
byte[] buf = new byte[1024];
int len;
while ((len = fis.read(buf)) > 0) {
fos.write(buf, 0, len);
}
fis.close();
fos.close();
}
catch (Exception e)
{
e.printStackTrace();
}
videoUri = data.getData();
}
}
}
【问题讨论】:
标签: java android video mediastore