我使用gallery从手机中选择视频和图像并将其加载到应用程序。 我使用代码:
Intent photoPickerIntent = null;
if (Build.VERSION.SDK_INT < 19) {
photoPickerIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
photoPickerIntent.setType("image/* video/*");
} else {
photoPickerIntent = new Intent(Intent.ACTION_GET_CONTENT);
photoPickerIntent.setType("image/* video/*");
}
一切都很好,但是当我进入内部存储/ DCIM时,我只能选择图像。视频文件是灰色的,我无法选择它们。有什么想法吗?
答案 0 :(得分:0)
试试此代码
private static final int VIDEO_GALLERY_REQUEST = 2;
Uri video_uri;
addVideoButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent galleryIntent = new Intent(Intent.ACTION_GET_CONTENT);
galleryIntent.setType("video/");
startActivityForResult(galleryIntent,VIDEO_GALLERY_REQUEST);
}
});
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == VIDEO_GALLERY_REQUEST && resultCode == -1){
video_uri = data.getData();
}
}
答案 1 :(得分:0)
启动意图:
Intent pickIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
pickIntent.setType("image/* video/*");
startActivityForResult(pickIntent, IMAGE_PICKER_SELECT);
检查视频和图片:
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
Uri selectedMediaUri = data.getData();
if (selectedMediaUri.toString().contains("images")) {
//Your image
} else if (selectedMediaUri.toString().contains("video")) {
//Your video
}
}
答案 2 :(得分:0)
此代码解决了我的问题:
if (Build.VERSION.SDK_INT < 19) {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/* video/*");
startActivityForResult(Intent.createChooser(intent, getResources().getString(R.string.select_picture)), SELECT_GALLERY);
} else {
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("image/*");
intent.putExtra(Intent.EXTRA_MIME_TYPES, new String[] {"image/*", "video/*"});
startActivityForResult(intent, SELECT_GALLERY_KITKAT);
}
这一行:
intent.putExtra(Intent.EXTRA_MIME_TYPES, new String[] {"image/*", "video/*"});