光标图像始终返回null [英] Cursor for an Image always returns null

查看:334
本文介绍了光标图像始终返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从我的相机存储的图片是这样的:

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);

其中, CameraActivity.DIR_PICTURES com.korcholis.testapp /图片。没什么特别的,在我看来。问题是当我试图让这个形象的一些信息。其他地方在我的code:

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());
}

其他人登录的回报<$c$c>content:///storage/emulated/0/com.korcholis.testapp/pictures/1aaf2e587kg519cejk88ch6hle372.png,这是正常的,但光标是 cursor.moveToFirst()。它看起来像光标找不到图像。然而,进入通过文件管理器的存储时,图像很容易在正确的文件夹中找到。我还检查过该文件时使用文件确实存在:// ,以及它的作用。我在做什么错了?

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?

修改2013年5月8日:
我已经把寻找一个解决方案,但是这看起来是不可能的。我读过在文件的其他线程:// 不是一个足够好的URI来查找使用 getContentResolver(),所以我尝试使用内容:// 来代替。这一点,尽管我的努力,并不如预期的那么顺利。我编辑的最后codeblock将到我使用的是当前code。我甚至试着将它添加到库中,因此它可以算作一个项目在解决内容列表。

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

推荐答案

而不是使用游标,因为你只需要找到一个图像的orienataion你可以这样做的:

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 
}

这篇关于光标图像始终返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆