Cet exemple utilise une chaîne de cheminfichier complet à votre 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;
}
un exemple supplémentaire où vous utilisez uri (Source: smoothie par Lu Rocha 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;
}
par exemple, la recherche de AlbumArt pour la piste avec id = 3, en utilisant la méthode uri:
imageUri = content://media/external/audio/albumart/3
sArtworkUri=content://media/external/audio/albumart
Quelle est votre valeur albumPath? c'est nul? – redAllocator
non. il a un chemin approprié, comme mentionné dans la question. –
Vous devez demander la permission. Vous n'êtes pas obligé d'obtenir le chemin. s'il vous plaît le relire Le rapide est d'abaisser targetApi à 22 (fichier build.gradle). – redAllocator