我想从画廊创建一个图片选择器。我使用代码
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);
我的问题是在这个活动和视频文件显示。是否有一种方法可以过滤显示的文件,以便在此活动中不显示视频文件?
当前回答
你可以做的比下面的回答更简单:
Uri Selected_Image_Uri = data.getData();
ImageView imageView = (ImageView) findViewById(R.id.loadedimg);
imageView.setImageURI(Selected_Image_Uri);
其他回答
如果你只是寻找图像和多重选择。
看一次https://stackoverflow.com/a/15029515/1136023
这对将来很有帮助。我个人觉得使用MultipleImagePick很棒。
Kotlin:当你想提示用户时,打开ACTION_GET_CONTENT事件:
val intent = Intent(Intent.ACTION_GET_CONTENT).apply { type = "image/*" }
startActivityForResult(intent, 9998)
当用户选择了一张图片后,在Activity的onActivityResult函数中处理该事件。作为一个例子,我在一个ImageView中显示它,并将它存储在应用程序缓存中:
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == 9998) {
val uri: Uri = data?.data ?: return
val bytes = contentResolver.openInputStream(uri)?.readBytes() ?: return
imageView.setImageBitmap(BitmapFactory.decodeByteArray(bytes, 0, bytes.size))
File("$cacheDir/imgPicked").writeBytes(bytes) // if needed: store to cache
}
}
理想情况下,将9998替换为应用程序使用的一些内部请求代码。这只是为了区分回调与您自己的请求。
与getParcelable("data")不同,它不需要任何权限。
注意,这不会处理设置它的图像上的Exif旋转位,因此一些图像最终会出现不正确的方向(Kotlin解决方案)。
对于只从本地选择添加这个:
i.putExtra(Intent.EXTRA_LOCAL_ONLY,true)
这个工作得很好:
val i = Intent(Intent.ACTION_GET_CONTENT,MediaStore.Images.Media.EXTERNAL_CONTENT_URI)
i.type = "image/*"
i.putExtra(Intent.EXTRA_LOCAL_ONLY,true)
startActivityForResult(Intent.createChooser(i,"Select Photo"),pickImageRequestCode)
有时,您无法从所选择的图片中获取文件。 这是因为选择一个来自谷歌+,Drive, Dropbox或任何其他提供商。
最好的解决方案是让系统通过Intent来选择内容。ACTION_GET_CONTENT并使用内容提供程序获取结果。
您可以按照下面的代码或查看我更新的要点。
public void pickImage() {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
startActivityForResult(intent, PICK_PHOTO_FOR_AVATAR);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_PHOTO_FOR_AVATAR && resultCode == Activity.RESULT_OK) {
if (data == null) {
//Display an error
return;
}
InputStream inputStream = context.getContentResolver().openInputStream(data.getData());
//Now you can do whatever you want with your inpustream, save it as file, upload to a server, decode a bitmap...
}
}
OnActivityResult方法已弃用
val singleImageResultLauncher =
registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
if (result.resultCode == Activity.RESULT_OK) {
// There are no request codes
val data: Intent? = result.data
val selectedImageUri: Uri? = data?.data
if (null != selectedImageUri) {
// Get the path from the Uri
val path = getPathFromURI(selectedImageUri)
findViewById<TextView>(R.id.textView).text = path
findViewById<ImageView>(R.id.imageView2).setImageURI(selectedImageUri)
}
}
}
findViewById<Button>(R.id.oneImageSelectBtn).setOnClickListener {
val intent = Intent()
intent.type = "image/*"
intent.action = Intent.ACTION_GET_CONTENT
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, false)
singleImageResultLauncher.launch(Intent.createChooser(intent, "Select Picture"))
}
private fun getPathFromURI(uri: Uri?): String {
var path = ""
if (contentResolver != null) {
val cursor = contentResolver.query(uri!!, null, null, null, null)
if (cursor != null) {
cursor.moveToFirst()
val idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DISPLAY_NAME)
path = cursor.getString(idx)
cursor.close()
}
}
return path
}