2013-05-07 96 views
0

我從我的相機存儲的圖片是這樣的:光標爲圖像始終返回null

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/pictures"。沒有什麼特別的,在我看來。當我嘗試獲取關於此圖像的一些信息時,問題就來了。在我的代碼別的地方:

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

其他登錄回報content:///storage/emulated/0/com.korcholis.testapp/pictures/1aaf2e587kg519cejk88ch6hle372.png,這是正常的,但光標是在nullcursor.moveToFirst()。它看起來像光標找不到圖像。但是,通過文件管理器進入存儲器時,可以在正確的文件夾中輕鬆找到該映像。我也檢查過使用file://時文件確實存在,它確實存在。我究竟做錯了什麼?

編輯5/8/2013: 我一直在尋找解決方案,但是這看起來不可能。我讀過其他線程file://是不夠好的Uri尋找使用getContentResolver(),所以我嘗試使用content://來代替。儘管我付出了努力,但這並沒有達到預期的效果。我編輯了最後一個代碼塊到我正在使用的當前代碼。我甚至嘗試將其添加到圖庫中,因此它可以作爲「已解決的內容列表」中的項目。

+0

你說你的時候,你要這個文件的一些信息,問題就來了。我明白你想在代碼中的其他地方找到關於圖像的東西。你到底需要什麼? – 2013-05-13 09:32:50

+0

我想抓住'MediaStore.Images.Media.ORIENTATION'爲你的[在這裏回答](http://stackoverflow.com/questions/16397279/implement-a-take-picture-crop-or-use-premade-意圖)。只是。我也能夠抓住圖片並在'ImageView'中設置爲可繪製的,所以圖片就存在了。 – Korcholis 2013-05-13 09:35:49

回答

1

而不是使用遊標,因爲你只需要找到一個圖像的orienataion你可以這樣做的:

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 
} 
+0

hello @kushwaha我需要找到圖像的方向,並使用'ExifInterface'總是返回給我0.你能幫助我爲什麼它總是返回零。 – Dory 2013-07-22 10:56:10

+0

此方法僅適用於相機應用程序使用exif數據保存圖片的情況,這在當今非常少見。如果沒有EXIF,則返回'null'。 – 2013-12-09 12:45:20