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".