Не могу добавить обложку альбома / уменьшенное изображение в просмотр изображений
Я новичок в java, и я не совсем уверен, что я должен делать. Я пытаюсь добавить обложку для каждой песни в список переработчиков. Если я уберу строку "albumCursor.moveToNext()" после "while{ ", это добавит одно и то же изображение к каждой песне. Но если я оставлю это, приложение вылетает. Я думаю, что это дает сбой, потому что все эскизы, которые он создает, имеют одинаковые имена, но я не могу понять, как это изменить.
public void getSongList() {
//retrieve song info
ContentResolver musicResolver = getActivity() .getContentResolver();
Uri musicUri = android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
Uri albumUri = android.provider.MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI;
Cursor musicCursor = musicResolver.query(musicUri, null, null, null, null);
Cursor albumCursor = musicResolver.query(albumUri, null, null, null, null);
songList = new ArrayList<Song>();
if(musicCursor!=null && musicCursor.moveToFirst()
&& albumCursor!=null && albumCursor.moveToFirst()){
//get columns
int titleColumn = musicCursor.getColumnIndex
(MediaStore.Audio.Media.TITLE);
int idColumn = musicCursor.getColumnIndex
(MediaStore.Audio.Media._ID);
int artistColumn = musicCursor.getColumnIndex
(MediaStore.Audio.Media.ARTIST);
int durationColumn = musicCursor.getColumnIndex
(MediaStore.Audio.Media.DURATION);
int albumColumn = musicCursor.getColumnIndex
(MediaStore.Audio.Media.ALBUM);
int albumIdColumn = albumCursor.getColumnIndex
(MediaStore.Audio.Albums._ID);
int thumbColumn = albumCursor.getColumnIndex
(MediaStore.Audio.Albums.ALBUM_ART);
//add songs to list
do {
long thisAId = albumCursor.getLong(albumIdColumn);
String img = albumCursor.getString(thumbColumn);
File imgFile = new File (img);
Bitmap thisThumb = (ThumbnailUtils.extractThumbnail(BitmapFactory.decodeFile(imgFile.getAbsolutePath()), 40, 40));
long thisId = musicCursor.getLong(idColumn);
String thisTitle = musicCursor.getString(titleColumn);
String thisArtist = musicCursor.getString(artistColumn);
long thisDuration = musicCursor.getLong(durationColumn);
String thisAlbum = musicCursor.getString(albumColumn);
//Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
//int thisThumb = albumCursor.getInt(thumbColumn);
songList.add(new Song(thisId, thisTitle, thisArtist, thisDuration, thisAlbum, thisThumb, thisAId));
}
while (musicCursor.moveToNext() && albumCursor.moveToNext());
}
}
Я хочу, чтобы это добавило произведение искусства ко всем песням, если вам, ребята, нужна дополнительная информация или код, просто спросите
12-11 20:59:45.723 6137-6137/com.reaper.frag E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.reaper.frag, PID: 6137
java.lang.NullPointerException
at java.io.File.<init>(File.java:262)
at com.reaper.frag.Fragment1.getSongList(Fragment1.java:168)
at com.reaper.frag.Fragment1.onCreateView(Fragment1.java:88)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:2261)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1419)
at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1750)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1819)
at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:797)
at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2590)
at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2377)
at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2332)
at android.support.v4.app.FragmentManagerImpl.execSingleAction(FragmentManager.java:2209)
at android.support.v4.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:649)
at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:145)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1238)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1086)
at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1616)
at android.view.View.measure(View.java:21040)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6460)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:758)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:640)
at android.view.View.measure(View.java:21040)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6460)
at android.support.design.widget.CoordinatorLayout.onMeasureChild(CoordinatorLayout.java:719)
at android.support.design.widget.CoordinatorLayout.onMeasure(CoordinatorLayout.java:791)
at android.view.View.measure(View.java:21040)
at com.sothree.slidinguppanel.SlidingUpPanelLayout.onMeasure(SlidingUpPanelLayout.java:803)
at android.view.View.measure(View.java:21040)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6460)
at android.support.design.widget.CoordinatorLayout.onMeasureChild(CoordinatorLayout.java:719)
at android.support.design.widget.CoordinatorLayout.onMeasure(CoordinatorLayout.java:791)
at android.view.View.measure(View.java:21040)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6460)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:139)
at android.view.View.measure(View.java:21040)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6460)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:758)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:640)
at android.view.View.measure(View.java:21040)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6460)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at android.view.View.measure(View.java:21040)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6460)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:758)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:640)
at
1 ответ
Я починил это:). Так что, если у кого-то есть такая же проблема, я разместил свое решение ниже. Приложение загружает все фотографии, но это только начало
public void getSongList() {
//retrieve song info
ContentResolver musicResolver = getContentResolver();
Uri musicUri = android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
Cursor musicCursor = musicResolver.query(musicUri, null, null, null, null);
Bitmap thisThumb = null;
byte[] art;
songList = new ArrayList<Song>();
if(musicCursor!=null && musicCursor.moveToFirst()){
//get columns
int titleColumn = musicCursor.getColumnIndex
(android.provider.MediaStore.Audio.Media.TITLE);
int idColumn = musicCursor.getColumnIndex
(android.provider.MediaStore.Audio.Media._ID);
int artistColumn = musicCursor.getColumnIndex
(android.provider.MediaStore.Audio.Media.ARTIST);
int durationColumn = musicCursor.getColumnIndex
(android.provider.MediaStore.Audio.Media.DURATION);
int albumColumn = musicCursor.getColumnIndex
(android.provider.MediaStore.Audio.Media.ALBUM);
int columnIndex = musicCursor.getColumnIndexOrThrow
(MediaStore.Audio.Media.DATA);
//add songs to list
do {
String pathId = musicCursor.getString(columnIndex);
Log.d(this.getClass().getName(), "path id=" + pathId);
metaRetriver.setDataSource(pathId);
try {
art = metaRetriver.getEmbeddedPicture();
BitmapFactory.Options opt = new BitmapFactory.Options();
opt.inSampleSize = 2;
thisThumb = (BitmapFactory .decodeByteArray(art, 0, art.length,opt));
//Bitmap thisAThumb = (ThumbnailUtils.extractThumbnail(BitmapFactory.decodeFile(imgFile.getAbsolutePath()), 40, 40));
System.out.println("art `" + thisThumb+"`");
} catch (Exception e) {
//imgAlbumArt.setBackgroundColor(Color.GRAY);
}
long thisId = musicCursor.getLong(idColumn);
String thisTitle = musicCursor.getString(titleColumn);
String thisArtist = musicCursor.getString(artistColumn);
String thisDuration = musicCursor.getString(durationColumn);
String thisAlbum = musicCursor.getString(albumColumn);
songList.add(new Song(thisId, thisTitle, thisArtist, thisDuration, thisAlbum, thisThumb));
}
while (musicCursor.moveToNext());
}
}