问题
I took a picture using MediaStore and saved it. I can see the image in my Gallery. However when I try to set the image bitmap to my ImageView, it becomes blank.
String abspath;
public void getPicture(View view) {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
File imageFile = new File(PATH + "/image.jpg");
abspath = imageFile.getAbsolutePath().toString();
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(imageFile));
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse(abspath)))
startActivityForResult(cameraIntent, RESULT_CAMERA);
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
setContentView(R.layout.activity);
if(requestCode == RESULT_CAMERA) {
// open the image file
Bitmap bitmap = BitmapFactory.decodeFile(abspath);
ImageView imageView = (ImageView)findViewById(R.id.imageView);
imageView.setImageBitmap(bitmap); //imageView is now blank, although my saved image is not a blank image
//imageView.setImageURI(Uri.fromFile(new File(abspath))); same result
}
}
回答1:
I don't know but there are some changes while getting image from Camera as part of onActivityResult()
in API version 17.you are trying by getting the image by using imagePath. But actually camera doesn't store it by given name.It uses the its own mechanism to store the image.You can see by going into gallery. Image name will be different than what you had given. As recommended It's better to use ContentResolver to query the image which is last captured.You can modify the query as per your requirement. Below is just an example.
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PIC_CAMERA_IMAGE && resultCode == RESULT_OK) {
Uri selectedImage=null;
// Works on API 16 or Below
if(null!=data)
{
selectedImage = data.getData();
}
// For API 17
if(null==selectedImage)
{
final ContentResolver cr = getContentResolver();
final String[] p1 = new String[] {
MediaStore.Images.ImageColumns._ID,
MediaStore.Images.ImageColumns.DATE_TAKEN
};
Cursor c1 = cr.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, p1, null, null, p1[1] + " DESC");
if ( c1.moveToFirst() ) {
String uristringpic = "content://media/external/images/media/" +c1.getInt(0);
selectedImage = Uri.parse(uristringpic);
}
c1.close();
}
}
}
回答2:
In my own code, I make the following calls before attempting to read from the media URI:
activity.getContentResolver().notifyChange(mediaUri, null);
Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
intent.setData(mediaUri);
activity.sendBroadcast(intent);
This is an attempt to assure that the system is correctly updated to resolve the content URI.
Edit: Sorry, I noticed you're attempting to read from the file path and not the URI. I'm not aware of why that might fail (other than permissions issues), but maybe you could try accessing the file via the URI obtained from URI.fromFile(imageFile).
回答3:
This works for me:
Intent intent = new Intent(
MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT,
mCapturedImageURI);
startActivityForResult(intent, TAKE_PICTURE)
followed by:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
String imagePath = null;
if (requestCode == TAKE_PICTURE)
if (resultCode == Activity.RESULT_OK) {
imagePath = getPath(mCapturedImageURI);
}
}
if (imagePath != null) {
// TODO: to this from asynctask
ExifInterface exif = null;
try {
exif = new ExifInterface(imagePath);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
int orientation = exif.getAttributeInt(
ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_NORMAL);
mBabymScaledPhoto = mApp.getBabyBitmapFromPath(imagePath,
orientation);
ImageView iv = (ImageView) fragView
.findViewById(R.id.welcome_iv_babyPhoto);
iv.setImageBitmap(mBabymScaledPhoto);
}
}
public String getPath(Uri uri) {
String[] projection = { MediaStore.Images.Media.DATA };
CursorLoader cursorLoader = new CursorLoader(fragView.getContext(),
uri, projection, null, null, null);
Cursor cursor = cursorLoader.loadInBackground();
if (cursor != null) {
// HERE YOU WILL GET A NULLPOINTER IF CURSOR IS NULL
// THIS CAN BE, IF YOU USED OI FILE MANAGER FOR PICKING THE MEDIA
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
} else
return null;
}
来源:https://stackoverflow.com/questions/17682604/imageview-setimagebitmap-is-blank