问题
I'm storing a picture from my Camera like this:
String newName = new BigInteger(130, new SecureRandom())
.toString(24);
File mydir = new File(Environment.getExternalStorageDirectory().getAbsolutePath(),
CameraActivity.DIR_PICTURES);
mydir.mkdirs();
fileWithinMyDir = new File(mydir, newName + ".png");
out = new FileOutputStream(fileWithinMyDir);
finalBitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
where CameraActivity.DIR_PICTURES
stands for "com.korcholis.testapp/pictures"
. Nothing special, in my opinion. The problem comes when I try to get some information about this image. Somewhere else in my code:
Uri selectedImage = Uri.fromFile(new File(sample.getPicture()));
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
mediaScanIntent.setData(selectedImage);
getSherlockActivity().sendBroadcast(mediaScanIntent); //Now it's in the Gallery
selectedImage = Uri.parse("content://"+(new File(sample.getPicture()).toString()));
String[] filePathColumn = {MediaStore.Images.ImageColumns.ORIENTATION};
Cursor cursor = getSherlockActivity().getContentResolver().query(selectedImage, filePathColumn, null, null, null);
if(cursor != null)
{
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String filePath = cursor.getString(columnIndex);
Log.i("ImageTest", cursor.getString(columnIndex));
cursor.close();
}
else
{
Log.i("ImageTest", selectedImage .toString());
}
The else Log returns content:///storage/emulated/0/com.korcholis.testapp/pictures/1aaf2e587kg519cejk88ch6hle372.png
, which is normal, but the cursor is null
at cursor.moveToFirst()
. It looks like the cursor can't find the image. However, when getting into the Storage through a file manager, the image is easily found in the correct folder. I've also checked that the file actually exists when using file://
, and it does. What am I doing wrong?
EDIT 5/8/2013:
I've kept looking for a solution, however this looks impossible. I've read in other threads that file://
isn't a good enough Uri to look for using getContentResolver()
, so I tried using content://
instead. This, despite my efforts, isn't going as well as expected. I edited the last codeblock to the current code I'm using. I've even tried adding it to the gallery, so it could count as an item in the "resolved content list".
回答1:
Instead of using a cursor, since you only need to find the orienataion of one image you could do this :
ExifInterface exif = new ExifInterface(selectedImage);
exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, -1);
if(orientation == 3 || orientation == 6 || orientation == 8 ){
Matrix matrix = new Matrix();
if (orientation == 6)
matrix.postRotate(90);
else if (orientation == 3)
matrix.postRotate(180);
else if (orientation == 8)
matrix.postRotate(270);
result = Bitmap.createBitmap(result, 0, 0, result.getWidth(), result.getHeight(), matrix, true); // rotating bitmap
}
来源:https://stackoverflow.com/questions/16413923/cursor-for-an-image-always-returns-null