2017-08-17 58 views
0
ContentResolver contentResolver1 = getActivity().getContentResolver(); 
Uri uri1 = MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI; 
String[] projection1 = new String[]{MediaStore.Audio.Albums._ID, MediaStore.Audio.Albums.ALBUM_ART}; 
String selection1 = MediaStore.Audio.Albums._ID + " = ?"; 
String[] selectionArgs1 = new String[]{String.valueOf(albumId)}; //albumId is MediaStore.Audio.Media.ALBUM_ID 

Cursor cursor1 = contentResolver1.query(uri1, projection1, selection1, selectionArgs1, null); 

if (cursor1 != null) { 
    if (cursor1.moveToFirst()) { 
     String albumPath = cursor1.getString(cursor1.getColumnIndex(MediaStore.Audio.Albums.ALBUM_ART)); 
     if (albumPath != null) 
      albumArt = BitmapFactory.decodeFile(albumPath);   
    } 
    cursor1.close(); 
} 

BitmapFactory.decodeFile(albumPath)的文件路徑獲取位圖給出了一個FileNotFoundException異常,即使albumPath有以下值 - /storage/emulated/0/Android/data/com.android.providers.media/albumthumbs/1502945757087從MediaStore.Audio.Albums.ALBUM_ART

this answer的評論說,有關使用文件路徑從BitmapFactory.decodeFile得到位圖(同樣的事情),這是不是在我的情況下工作。 如何使用上述路徑獲取位圖?

+0

你是什麼albumPath價值?它是空的? – redAllocator

+0

不。正如問題中提到的那樣,它有一條正確的道路。 –

+0

你必須申請許可。你不必走路。請再次閱讀 快速的是將targetApi降低到22(build.gradle文件)。 – redAllocator

回答

-2

您必須申請許可。 你不必走路。 請再讀一遍

快速之一是將targetApi降低到22(build.gradle文件)。

或使用新撫換權限模型:

Requesting Permissions at Run Time

+0

聯繫人和媒體庫如何關聯? –

+0

你必須檢查權限。請檢查一下。 – redAllocator

+0

問題不在於訪問外部存儲。它是關於從文件路徑獲取位圖的。 –

0

試試這個:

Cursor cursor = getActivity().managedQuery(MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI, 
        new String[] {MediaStore.Audio.Albums._ID, MediaStore.Audio.Albums.ALBUM_ART}, 
        MediaStore.Audio.Albums._ID+ "=?", 
        new String[] {String.valueOf(id_alnum)}, 
        null); 

    if (cursor.moveToFirst()) { 
     String path = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Albums.ALBUM_ART)); 
     //TODO code 
    } 
+0

已經晚了。變量'路徑'有一個路徑,但'BitmapFactory.decodeFile(路徑)'給出一個文件沒有發現異常。 –

-1

此示例使用完整的文件路徑的字符串到您albumart

 public static Bitmap decodeSampledBitmapFromFile(String picture, int reqWidth, int reqHeight) { 

    // First decode with inJustDecodeBounds=true to check dimensions 
    final BitmapFactory.Options options = new BitmapFactory.Options(); 
    options.inJustDecodeBounds = true; 
    BitmapFactory.decodeFile(picture, options); 

    // Calculate inSampleSize 
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); 

    // Decode bitmap with inSampleSize set 
    options.inJustDecodeBounds = false; 
    return BitmapFactory.decodeFile(picture, options); 
} 


    public static int calculateInSampleSize(
     BitmapFactory.Options options, int reqWidth, int reqHeight) { 
    // Raw height and width of image 
    final int height = options.outHeight; 
    final int width = options.outWidth; 
    int inSampleSize = 1; 

    if (height > reqHeight || width > reqWidth) { 

     final int halfHeight = height/2; 
     final int halfWidth = width/2; 

     // Calculate the largest inSampleSize value that is a power of 2 and keeps both 
     // height and width larger than the requested height and width. 
     while ((halfHeight/inSampleSize) >= reqHeight 
       && (halfWidth/inSampleSize) >= reqWidth) { 
      inSampleSize *= 2; 
     } 
    } 

    return inSampleSize; 
} 

一些額外的例子,你使用uri的(來源:魯路易斯盧CAS羅沙):

private Bitmap decodeSampledBitmapFromResource(Uri imageUri, int reqWidth, 
     int reqHeight) { 
    InputStream is = null; 
    try { 
     is = mContext.getContentResolver().openInputStream(imageUri); 
     final BitmapFactory.Options options = new BitmapFactory.Options(); 
     options.inJustDecodeBounds = true; 
     BitmapFactory.decodeStream(is, null, options); 
     options.inSampleSize = calculateInSampleSize(options, reqWidth, 
       reqHeight); 
     options.inJustDecodeBounds = false; 
     is = mContext.getContentResolver().openInputStream(imageUri); 
     return BitmapFactory.decodeStream(is, null, options); 
    } catch (FileNotFoundException e) { 
    //  e.printStackTrace(); 
     return null; 
    } finally { 
     try { 
      if (is != null) { 
       is.close(); 
      } 
     } catch (IOException e) { 
    //  e.printStackTrace(); 
     } 
    } 
} 
@Override 
public Bitmap loadItem(Long id) { 
    Uri sArtworkUri = Uri.parse("content://media/external/audio/albumart"); 
    Uri imageUri = Uri.withAppendedPath(sArtworkUri, String.valueOf(id)); 
    Resources res = mContext.getResources(); 
    int width = res.getDimensionPixelSize(R.dimen.image_width); 
    int height = res.getDimensionPixelSize(R.dimen.image_height); 

    Bitmap bitmap = null; 

    try { 
     bitmap = decodeSampledBitmapFromResource(imageUri, width, height); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 

    return bitmap; 
} 

例如,尋找albumart的軌道與ID = 3,使用URI方法:

imageUri = content://media/external/audio/albumart/3 
sArtworkUri=content://media/external/audio/albumart 
+0

你能否提供完整文件路徑的樣本字符串? –