[英]How to get Bitmap from MediaStore.Images.Media.getBitmap(contentResolver, uri)?
I am having difficulties with retrieving a bitmap from my uri.我在从 uri 检索位图时遇到困难。 I am trying to fetch the image taken from camera after opening the camera via an Intent.
我试图通过 Intent 打开相机后获取从相机拍摄的图像。
try {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
imageUri = FileProvider.getUriForFile(mContext, mContext.getApplicationContext().getPackageName(), createImageFile());
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri); // set the image file name
}
startActivityForResult(intent, PERMISSION_REQUEST_CODE_CAMERA);
} catch (ActivityNotFoundException e) {
e.printStackTrace();
}
The imageUri when logged is记录时的 imageUri 是
content://com.example/external_files/Pictures/CustomFolder/15122017_photo_115344.jpg
content://com.example/external_files/Pictures/CustomFolder/15122017_photo_115344.jpg
After the camera is opened and the picture taken, my onActivityResult is called.打开相机并拍摄照片后,我的 onActivityResult 被调用。 Because I use MediaStore.EXTRA_OUTPUT I do not receive anything in my data object it is null.
因为我使用 MediaStore.EXTRA_OUTPUT 我没有在我的数据对象中收到任何东西,它是空的。 I try to use MediaStore.Images.Media.getBitmap() to retrieve the bitmap.
我尝试使用 MediaStore.Images.Media.getBitmap() 来检索位图。 I pass in the imageUri.
我传入了 imageUri。 I believe this may be incorrect because I receive an exception.
我相信这可能是不正确的,因为我收到了一个例外。
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PERMISSION_REQUEST_CODE_CAMERA) {
if (resultCode == Activity.RESULT_OK) {
Bitmap bitmap;
try {
bitmap = MediaStore.Images.Media.getBitmap(mContext.getContentResolver(), imageUri);
ivProfilePhoto.setImageBitmap(bitmap);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else if (resultCode == Activity.RESULT_CANCELED){
}
}
The exception I receive is我收到的例外是
java.io.FileNotFoundException: No such file or directory
java.io.FileNotFoundException: 没有这样的文件或目录
EDIT:编辑:
I can hang on to the File, but this does not seem to work either.我可以保留文件,但这似乎也不起作用。 Here is what I have tried.
这是我尝试过的。 I updated my intent to be the following
我更新了我的意图如下
try {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
mFile = createImageFile();
imageUri = FileProvider.getUriForFile(mContext, mContext.getApplicationContext().getPackageName(), mFile);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri); // set the image file name
}
startActivityForResult(intent, PERMISSION_REQUEST_CODE_CAMERA);
} catch (ActivityNotFoundException e) {
e.printStackTrace();
}
Then in onActivityResult, I do the following然后在 onActivityResult 中,我执行以下操作
Bitmap bitmap = BitmapFactory.decodeFile(mFile.getPath());
ivProfilePhoto.setImageBitmap(bitmap);
Nothing gets set for the image.没有为图像设置任何内容。 Also, for your FYI this is how I am creating my directory and setting my file image name.
另外,仅供参考,这就是我创建目录和设置文件图像名称的方式。
private File createImageFile() {
File imgFileDir = getDir();
if (!imgFileDir.exists() && !imgFileDir.mkdirs()) {
Logger.e(TAG, "Directory does not exist");
imgFileDir.mkdirs();
}
Logger.e("TEST", "imgFileDir= " + imgFileDir);
// Locale.US to get local formatting
SimpleDateFormat timeFormat = new SimpleDateFormat("hhmmss", Locale.US);
SimpleDateFormat dateFormat = new SimpleDateFormat("ddMMyyyy", Locale.US);
String time = timeFormat.format(new Date());
String date = dateFormat.format(new Date());
String photoFile = date + "_photo_" + time + ".jpg";
filename = imgFileDir.getPath() + File.separator + photoFile;
Logger.i(TAG, "time(hhmmss): " + time + " date(ddmmyyyy): " + date + "\nfilename: "
+ filename + " photoFile: " + photoFile);
return new File(filename);
}
private File getDir() {
File sdDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
return new File(sdDir, "CustomFolder");
}
Ideally, switch to using an image-loading library (eg, Glide, Picasso), so that your I/O and bitmap processing can be performed on a background thread.理想情况下,切换到使用图像加载库(例如 Glide、Picasso),以便您的 I/O 和位图处理可以在后台线程上执行。
Beyond that, hold onto the File
object, not the Uri
, and load the image using the File
(eg, BitmapFactory.decodeFile()
).除此之外,保持
File
对象,而不是Uri
,并使用File
(例如, BitmapFactory.decodeFile()
)加载图像。 This will be faster, as there will be no ContentProvider
overhead.这会更快,因为没有
ContentProvider
开销。
这对我有用
val bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), uri);
As per my understanding you want to fetch the uri from the bitmap so considerably this code works perfactably fine :-根据我的理解,您想从位图中获取 uri 相当多,这段代码确实可以正常工作:-
public Uri getImageUri(Context inContext, Bitmap inImage) {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
String path = MediaStore.Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
return Uri.parse(path);
}
ADD this添加这个
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT,MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, 1);
}
when you will replace your code with this above code then automatically your this当你用上面的代码替换你的代码时,然后自动你的这个
public void onActivityResult(int requestCode, int resultCode,
@Nullable Intent data){}
Method will Start working方法将开始工作
//No Need to write this code in onclick method
Intent intent=new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT)
startActivityForResult(intent,1);
Toast.makeText(getContext(), "image"+intent, Toast.LENGTH_SHORT).show();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.