我想从画廊创建一个图片选择器。我使用代码
intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, TFRequestCodes.GALLERY);
我的问题是在这个活动和视频文件显示。是否有一种方法可以过滤显示的文件,以便在此活动中不显示视频文件?
我想从画廊创建一个图片选择器。我使用代码
intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, TFRequestCodes.GALLERY);
我的问题是在这个活动和视频文件显示。是否有一种方法可以过滤显示的文件,以便在此活动中不显示视频文件?
当前回答
2021 Kotlin解决方案与新版本的Fragment:
dependencies {
implementation "androidx.fragment:fragment:1.3.3"
}
class YourFragment : Fragment() {
private val fileChooserContract = registerForActivityResult(ActivityResultContracts.GetContent()) { imageUri ->
if (imageUri != null) {
// imageUri now contains URI to selected image
}
}
// ...
fun openFileChooser() {
fileChooserContract.launch("image/*")
}
}
其他回答
public void FromCamera() {
Log.i("camera", "startCameraActivity()");
File file = new File(path);
Uri outputFileUri = Uri.fromFile(file);
Intent intent = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(intent, 1);
}
public void FromCard() {
Intent i = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, 2);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 2 && resultCode == RESULT_OK
&& null != data) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
bitmap = BitmapFactory.decodeFile(picturePath);
image.setImageBitmap(bitmap);
if (bitmap != null) {
ImageView rotate = (ImageView) findViewById(R.id.rotate);
}
} else {
Log.i("SonaSys", "resultCode: " + resultCode);
switch (resultCode) {
case 0:
Log.i("SonaSys", "User cancelled");
break;
case -1:
onPhotoTaken();
break;
}
}
}
protected void onPhotoTaken() {
// Log message
Log.i("SonaSys", "onPhotoTaken");
taken = true;
imgCapFlag = true;
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 4;
bitmap = BitmapFactory.decodeFile(path, options);
image.setImageBitmap(bitmap);
}
对于Kotlin使用新的ActivityResultContracts,因为startActivityForResult是废弃的:
private val mSelectedPicDataResult = registerForActivityResult(
ActivityResultContracts.StartActivityForResult()
) { result ->
if (result.resultCode == Activity.RESULT_OK) {
val selectedPicUri = result.data?.data
//use your selected pic
}
}
private fun startSelectPic() {
val intent = Intent(Intent.ACTION_OPEN_DOCUMENT)
intent.addCategory(Intent.CATEGORY_OPENABLE)
intent.type = "image/*"
mSelectedPicDataResult.launch(intent)
}
2021 Kotlin解决方案与新版本的Fragment:
dependencies {
implementation "androidx.fragment:fragment:1.3.3"
}
class YourFragment : Fragment() {
private val fileChooserContract = registerForActivityResult(ActivityResultContracts.GetContent()) { imageUri ->
if (imageUri != null) {
// imageUri now contains URI to selected image
}
}
// ...
fun openFileChooser() {
fileChooserContract.launch("image/*")
}
}
如果你只是寻找图像和多重选择。
看一次https://stackoverflow.com/a/15029515/1136023
这对将来很有帮助。我个人觉得使用MultipleImagePick很棒。
再见startActivityForResult ()
现在AndroidX活动的正确方法是活动结果api,这是谷歌强烈推荐的方法
private val selectImageFromGalleryResult = registerForActivityResult(ActivityResultContracts.GetContent()) { uri: Uri? ->
uri?.let { previewImage.setImageURI(uri) }
}
需要时只需调用selectImageFromGallery()
private fun selectImageFromGallery() = selectImageFromGalleryResult.launch("image/*")