因为项目中涉及到获取mp3文件专辑封面,从网上找了一些资料,整理了一个demo。
挺简单的一种实现方式。记录一下。
步骤:首先获取手机中MP3文件路径,
然后调用 MediaMetadataRetriever 实现。
MainActivity代码如下。
package com.example.chy.loadmp3cover;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import android.Manifest;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.MediaMetadataRetriever;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import java.io.File;
public class MainActivity extends AppCompatActivity {
private static String[] PERMISSIONS_STORAGE = {android.Manifest.permission.READ_EXTERNAL_STORAGE,
android.Manifest.permission.WRITE_EXTERNAL_STORAGE}; //请求状态码 private static int REQUEST_PERMISSION_CODE = 2;
private static int REQUEST_PERMISSION_CODE = 2;
Button button;
ImageView image;
String str = "/storage/self/primary/moves.mp3";//从手机中获取的歌曲
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获取权限
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP) {
if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(MainActivity.this, PERMISSIONS_STORAGE, REQUEST_PERMISSION_CODE);
}
}
init();//初始化
}
public void init() {
button = findViewById(R.id.button);
image = findViewById(R.id.image);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
MainActivity.this.getWindow().setBackgroundDrawableResource(R.drawable.ic_launcher_background);//设置背景
if (!(new File(str)).exists()) {
Log.d("=======", "there is no songs named moves.mp3 , i am sad now ");
return;
}
loadCover(str);
}
});
}
private void loadCover(String path) {
MediaMetadataRetriever mediaMetadataRetriever = new MediaMetadataRetriever();
mediaMetadataRetriever.setDataSource(path);
byte[] cover = mediaMetadataRetriever.getEmbeddedPicture();
Bitmap bitmap = BitmapFactory.decodeByteArray(cover, 0, cover.length);
image.setImageBitmap(bitmap);
}
}
布局文件中就是一个button和imageview。
布局文件代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">
<Button
android:text="button"
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ImageView
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>