2017-08-17 2 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) gibt eine FileNotFoundException obwohl albumPath hat folgende wert- /storage/emulated/0/Android/data/com.android.providers.media/albumthumbs/1502945757087Erste Bitmap aus Dateipfad von MediaStore.Audio.Albums.ALBUM_ART

der Kommentar auf this answer sagt dasselbe über filepath mit Bitmap aus BitmapFactory.decodeFile(), die in meinem Fall nicht funktioniert zu bekommen. Wie wird der obige Pfad verwendet, um eine Bitmap zu erhalten?

+0

was ist Ihr albumPath Wert? es ist null? – redAllocator

+0

nein. es hat einen richtigen Pfad, wie in der Frage erwähnt. –

+0

Sie müssen um Erlaubnis bitten. Du musst keinen Weg finden. Bitte lesen Sie es erneut Die schnelle ist es, targetApi auf 22 (build.gradle Datei) zu senken. – redAllocator

Antwort

-2

Sie müssen um Erlaubnis bitten. Sie brauchen keinen Pfad zu bekommen. Bitte lesen Sie es erneut

Die schnelle ist es, targetApi auf 22 (build.gradle Datei) zu senken.

oder neue ask-for-Erlaubnis-Modell:

Requesting Permissions at Run Time

+0

Wie sind Kontakte und Mediastore überhaupt verwandt? –

+0

Sie müssen die Berechtigung prüfen. Überprüfen Sie bitte das. – redAllocator

+0

Die Frage betrifft auch nicht den Zugriff auf den externen Speicher. Es geht darum, eine Bitmap aus einem Dateipfad zu erhalten. –

0

Versuchen Sie folgendes:

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

gleichen Post. Variable 'Pfad' hat einen Pfad, aber' BitmapFactory.decodeFile (Pfad) 'gibt eine Datei nicht gefunden Ausnahme. –

-1

Dieses Beispiel eine Zeichenfolge des vollständigen Dateipfad verwendet, um Ihre 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; 
} 

einige zusätzliche Beispiele, wo Sie Uri's verwenden (Quelle: Smoothie von Lu cas Rocha):

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

zum Beispiel der Suche nach albumart für Track mit id = 3, die uri Methode:

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

können Sie eine Beispielzeichenfolge für den vollständigen Dateipfad angeben? –