我正在尝试获取音乐播放器应用程序中每首歌曲的专辑封面。但是除了黑屏,没有专辑封面,我什么也得不到。除了我从未听说过的日志猫错误之外,代码中似乎没有这样的错误。
这是包含按钮单击侦听器的Java文件。一旦我单击按钮在我的应用程序中,我在logcat中收到错误:培根1.java
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.os.Environment;
import android.support.design.widget.Snackbar;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
public class Bacon1 extends Activity {
private static final int MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE = 1;
private static int RESULT_LOAD_IMAGE = 1;
public ArrayList<HashMap<String, String>> songsList = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bacon1);
Button button4 = (Button) findViewById(R.id.button4);
button4.setOnClickListener(
new Button.OnClickListener()
{
public void onClick(View v) {
buttonClicked(v);
Intent p = new Intent(Bacon1.this,PlayListActivity.class);
startActivity(p);
}
});
}
private void buttonClicked(View view) {
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.READ_EXTERNAL_STORAGE) + ContextCompat
.checkSelfPermission(this,
Manifest.permission.INTERNET)
!= PackageManager.PERMISSION_GRANTED) {
Snackbar.make(view, "Permission not Granted, Requesting permission.", Snackbar.LENGTH_LONG).show();
if (ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.READ_EXTERNAL_STORAGE)) {
Snackbar.make(view, "We need permission to internal storage for displaying songs", Snackbar.LENGTH_LONG).show();
} else {
Snackbar.make(view, "Allow myapp3 to access this device's internal storage", Snackbar.LENGTH_LONG).show();
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE);
// MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
// app-defined int constant. The callback method gets the
// result of the request.
}
}
}
public void onClick2(View view) {
Intent i = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);
}
public void onClick4(View view) {
Intent viewIntent1 = new Intent(Intent.ACTION_VIEW);
File file = Environment.getExternalStorageDirectory();
viewIntent1.setDataAndType(Uri.fromFile(file), "video/*");
startActivity(Intent.createChooser(viewIntent1, null));
}
public void onClick5(View view) {
Intent viewIntent1 = new Intent(Intent.ACTION_VIEW);
File file = Environment.getExternalStorageDirectory();
viewIntent1.setDataAndType(Uri.fromFile(file), "zip/*");
startActivity(Intent.createChooser(viewIntent1, null));
}
public void onClick6(View view) {
Intent viewIntent1 = new Intent(Intent.ACTION_VIEW);
File file = Environment.getExternalStorageDirectory();
viewIntent1.setDataAndType(Uri.fromFile(file), "text/*");
startActivity(Intent.createChooser(viewIntent1, null));
}
public void onClick7(View view) {
Intent viewIntent1 = new Intent(Intent.ACTION_VIEW);
File file = Environment.getExternalStorageDirectory();
viewIntent1.setDataAndType(Uri.fromFile(file), "application/*");
startActivity(Intent.createChooser(viewIntent1, null));
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
switch (requestCode) {
case 1: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(Bacon1.this, "WRITE_CONTACTS granted", Toast.LENGTH_SHORT)
.show();
} else {
Toast.makeText(Bacon1.this, "WRITE_CONTACTS Denied", Toast.LENGTH_SHORT)
.show();
}
return;
}
// other 'case' lines to check for other
// permissions this app might request
}
}
}
这是日志猫:
07-11 17:11:04.222 13978-13978/com.example.dell_1.myapp3 E/ExtMediaPlayer-JNI: env->IsInstanceOf fails
07-11 17:11:04.222 13978-13978/com.example.dell_1.myapp3 E/MediaPlayer-JNI: JNIMediaPlayerFactory: bIsQCMediaPlayerPresent 0
07-11 17:11:04.222 13978-13978/com.example.dell_1.myapp3 E/ExtMediaPlayer-JNI: env->IsInstanceOf fails
07-11 17:11:04.222 13978-13978/com.example.dell_1.myapp3 E/MediaPlayer-JNI: JNIMediaPlayerFactory: bIsQCMediaPlayerPresent 0
这是负责将歌曲显示为列表视图的文件:PlayListActivity.java
package com.example.dell_1.myapp3;
import android.app.Activity;
import android.database.Cursor;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import java.io.File;
import java.io.IOException;
import com.bumptech.glide.Glide;
public class PlayListActivity extends Activity {
private String[] mAudioPath;
private MediaPlayer mMediaPlayer;
private String[] mMusicList;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_play_list);
mMediaPlayer = new MediaPlayer();
ListView mListView = (ListView) findViewById(R.id.list);
mMusicList = getAudioList();
ArrayAdapter<String> mAdapter = new ArrayAdapter<>(this,
android.R.layout.simple_list_item_1, mMusicList);
mListView.setAdapter(mAdapter);
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View view, int arg2,
long arg3) {
try {
playSong(mAudioPath[arg2]);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
private String[] getAudioList() {
final Cursor mCursor = getContentResolver().query(
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
new String[]{MediaStore.Audio.Media.DISPLAY_NAME, MediaStore.Audio.Media.DATA}, null, null,
"LOWER(" + MediaStore.Audio.Media.TITLE + ") ASC");
int count = mCursor.getCount();
String[] songs = new String[count];
mAudioPath = new String[count];
int i = 0;
if (mCursor.moveToFirst()) {
do {
songs[i] = mCursor.getString(mCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME));
mAudioPath[i] = mCursor.getString(mCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA));
i++;
} while (mCursor.moveToNext());
}
mCursor.close();
return songs;
}
private void playSong(String path) throws IllegalArgumentException,
IllegalStateException, IOException {
setContentView(R.layout.activity_android_building_music_player);
Log.d("ringtone", "playSong :: " + path);
mMediaPlayer.reset();
mMediaPlayer.setDataSource(path);
//mMediaPlayer.setLooping(true);
mMediaPlayer.prepare();
mMediaPlayer.start();
asd();
}
public void asd() {
ImageView imageView = (ImageView) findViewById(R.id.coverart);
File music = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC);
// Tested with music from Windows 7's c:UsersPublicMusicSample Music
String filePath = new File(music, "I love you.mp3").getAbsolutePath();
Glide
.with(this)
.load(new AudioCover(filePath))
.placeholder(R.drawable.adele1)
.error(R.drawable.adele1)
.into(imageView);
}
}
这是 xml 文件:activity_play_list.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:divider="#242424"
android:dividerHeight="1dp"
android:listSelector="@drawable/list_selector" />
</LinearLayout>
activity_android_building_music_player.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<LinearLayout
android:id="@+id/player_header_bg"
android:layout_width="fill_parent"
android:layout_height="60dip"
android:layout_alignParentTop="true"
android:background="@layout/bg_player_header"
android:paddingLeft="5dp"
android:paddingRight="5dp">
<TextView
android:id="@+id/songTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_weight="1"
android:paddingLeft="50dp"
android:text="The Good, The Bad And The Ugly"
android:textColor="#04b3d2"
android:textSize="16dp"
android:textStyle="bold" />
<ImageButton
android:id="@+id/btnPlaylist"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:background="@null"
android:src="@drawable/btn_playlist" />
</LinearLayout>
<LinearLayout
android:id="@+id/songThumbnail"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/player_header_bg"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:gravity="center"
android:paddingBottom="10dp"
android:paddingTop="10dp">
<ImageView
android:id="@+id/coverart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
<LinearLayout
android:id="@+id/player_footer_bg"
android:layout_width="fill_parent"
android:layout_height="100dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:background="@layout/bg_player_footer"
android:gravity="center">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:weightSum="1">
<RelativeLayout
android:layout_width="300dp"
android:layout_height="match_parent">
<ImageButton
android:id="@+id/btnPrevious"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2.40"
android:layout_alignParentLeft="true"
android:paddingLeft="10dp"
android:background="@null"
android:src="@drawable/btn_previous" />
<ImageButton
android:id="@+id/btnPlay1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="@null"
android:src="@drawable/btn_play"
android:onClick="buttonAction1"/>
<ImageButton
android:id="@+id/btnNext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="250dp"
android:background="@null"
android:src="@drawable/btn_next" />
</RelativeLayout>
</LinearLayout>
</LinearLayout>
<SeekBar
android:id="@+id/songProgressBar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/player_footer_bg"
android:layout_alignLeft="@+id/timerDisplay"
android:layout_alignStart="@+id/timerDisplay"
android:layout_marginBottom="10dp"
android:paddingLeft="6dp"
android:paddingRight="6dp"
android:progressDrawable="@drawable/seekbar_progress"
android:thumb="@drawable/download8" />
<LinearLayout
android:id="@+id/timerDisplay"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="@id/songProgressBar"
android:layout_marginBottom="10dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp">
<TextView
android:id="@+id/songCurrentDurationLabel"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="left"
android:textColor="#eeeeee"
android:textStyle="bold" />
<TextView
android:id="@+id/songTotalDurationLabel"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:textColor="#04cbde"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="@id/timerDisplay"
android:gravity="center">
<ImageButton
android:id="@+id/btnRepeat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:background="@null"
android:src="@drawable/btn_repeat" />
<ImageButton
android:id="@+id/btnShuffle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:background="@null"
android:src="@drawable/btn_shuffle" />
</LinearLayout>
</RelativeLayout>
亲爱的程序员。我重建了这个项目,一切正常,直到你面对.zip
格式和/或Uri
部分。
我的建议:
更改.zip
预设。 还要检查您用过的SDK
。
我还发现,生成的Uri
无效。
viewIntent1.setDataAndType(Uri.fromFile(file), "zip/*");
它创建了一个无效的Uri
,请尝试使用.parse
类似的东西:
Uri file = Uri.parse("/zip");
PS:我认为它也与设备相关。有些人遇到问题,有些人没有。也许它可以帮助你。我认为写一个可能的解决方案比让这个问题死去要好。;)