将相册艺术显示在图像视图中

时间:2017-08-17 17:17:26

标签: java android android-mediaplayer

我想将相册艺术加载到图片视图中,但我的代码无效并且出错,如何解决我的错误?请帮忙。

ImageView imageView= (ImageView) findViewById(R.id.imageView);
    Uri sArtworkUri = Uri.parse("/sdcard/Sqlit/AndroidDownloadManager.mp3");
    if (sArtworkUri !=null){
        MediaMetadataRetriever mData=new MediaMetadataRetriever();
        mData.setDataSource(this,sArtworkUri);


        byte art[]=mData.getEmbeddedPicture();
        Bitmap image= BitmapFactory.decodeByteArray(art,0,art.length);
        imageView.setImageBitmap(image);

    }

错误

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.andriodtime.net/com.andriodtime.net.MainActivity}: java.lang.NullPointerException
                                                                 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
                                                                 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
                                                                 at android.app.ActivityThread.access$800(ActivityThread.java:135)
                                                                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
                                                                 at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                 at android.os.Looper.loop(Looper.java:136)
                                                                 at android.app.ActivityThread.main(ActivityThread.java:5001)
                                                                 at java.lang.reflect.Method.invokeNative(Native Method)
                                                                 at java.lang.reflect.Method.invoke(Method.java:515)
                                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
                                                                 at dalvik.system.NativeStart.main(Native Method)
                                                              Caused by: java.lang.NullPointerException
                                                                 at com.andriodtime.net.MainActivity.initView(MainActivity.java:91)
                                                                 at com.andriodtime.net.MainActivity.onCreate(MainActivity.java:35)

1 个答案:

答案 0 :(得分:0)

您的应用程序可能无权从SD卡读取。

您可以向清单添加以下权限 <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

https://developer.android.com/reference/android/Manifest.permission.html#READ_EXTERNAL_STORAGE

请注意,您可能必须在运行时请求此权限。