This problem occurs in android 6.0.1, but not in android 7.0. I have this class FileKit to take photo via camera and it has method uriToBitmap()
which turns Uri into Bitmap. taken_photo_uri
stores the taken photo's uri.
class FileKit{
public static Uri taken_photo_uri = null;
public static void takePhoto(RootActivity activity) {
open_camera_activity = activity;
if (ContextCompat.checkSelfPermission(MyApplicaiton.getContext(),
Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
activity.setPermissionRequestInterface(getContext());
ActivityCompat.requestPermissions(activity,
new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, REQ_READ_PHOTO);
} else {
File output_image = new File(MyApplicaiton.getContext().getExternalCacheDir(), "output_image.jpg");
try {
if (output_image.exists()) {
output_image.delete();
}
output_image.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
if (Build.VERSION.SDK_INT >= 24) {
taken_photo_uri = FileProvider.getUriForFile(MyApplicaiton.getContext(),
activity.getResources().getString(R.string.main_package),
output_image);
} else {
taken_photo_uri = Uri.fromFile(output_image);
}
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
intent.putExtra(MediaStore.EXTRA_OUTPUT, taken_photo_uri);
activity.startActivityForResult(intent, TAKE_PHOTO);
}
}
public static Bitmap uriToBitmap(Uri uri) {
Bitmap bitmap = null;
try {
bitmap = BitmapFactory.decodeStream(MyApplicaiton.getContext().getContentResolver().openInputStream(uri));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return bitmap;
}
Here's how I use it in a demo Activity:
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FileKit.takePhoto(activity);
}
});
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
switch (requestCode) {
case FileKit.TAKE_PHOTO:
if (resultCode == RESULT_OK) {
Bitmap bitmap = FileKit.uriToBitmap(FileKit.taken_photo_uri);
if (bitmap == null) {
GeneralUtil.showToast("null picture");
} else {
GeneralUtil.showToast("not null picture");
imageView.setImageBitmap(bitmap);
}
}
break;
}
}
Things are good in android 7.0 but uriToBitmap(Uri uri)
returns null in android 6.0.1. How to fix it? Thanks!
Take a look in
takePhoto(){
if (Build.VERSION.SDK_INT >= 24) {
taken_photo_uri = FileProvider.getUriForFile(MyApplicaiton.getContext(),
activity.getResources().getString(R.string.main_package),
output_image);
} else {
taken_photo_uri = Uri.fromFile(output_image);
}
}
Your version check calls FileProvider.getUriForFile() on Nougat and above. From android docs ( https://developer.android.com/reference/android/os/Build.VERSION_CODES ), getUriForFile() returns a Uri as expected. However, on Marshmallow and below, you make a call to Uri.fromFile() which, again according to android docs ( https://developer.android.com/reference/android/net/Uri.html#fromFile(java.io.File) ) returns a Uri prefixed with "file://."
Without knowing much about Uri or Android specifically, I'd say that might be the issue.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.