访问图库和相机图像时,应用程序崩溃

时间:2017-01-11 11:55:15

标签: android android-6.0-marshmallow

我必须创建一个应用程序,将一些数据发送给其他人,其中包括从图库中选择图像并使用相机拍照这里是我的代码

ivattach.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            startActivityForResult(galleryPhoto.openGalleryIntent(),GALLER_REQUEST);
        }
    });
    ivCamera.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            try {
                getFileUri();


                startActivityForResult(cameraPhoto.takePhotoIntent(),CAMERA_REQUEST);
                cameraPhoto.addToGallery();
            } catch (IOException e) {
                Toast.makeText(getApplicationContext(),"Somthing Wrong",Toast.LENGTH_SHORT).show();
            }
        }
    });
}
@Override
protected void onActivityResult(int requestCode, int resultCode, final Intent data) {
   // try {
        if (resultCode == RESULT_OK) {
            if (requestCode == CAMERA_REQUEST) {

                photopath = cameraPhoto.getPhotoPath();
                Log.d(TAG, photopath);
                photoname.setText(photopath);
            } else if (requestCode == GALLER_REQUEST) {
                Uri uri =  data.getData();

                System.out.println("this is gallery image "+uri);
                galleryPhoto.setPhotoUri(uri);
                gphotopath = galleryPhoto.getPath();
                filename.setText(gphotopath);
            }
        }

代码在KitKat和棒棒糖中运行但是当使用三星note5和marshmallow时应用程序崩溃我不知道我的代码发生了什么,请提前帮助我找到解决此问题的方法 这是我得到的错误

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.example.jijoabraham.informe, PID: 2525
              java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=22131, result=-1, data=Intent { dat=content://com.android.providers.media.documents/document/image:33233 flg=0x1 }} to activity {com.example.jijoabraham.informe/com.example.jijoabraham.informe.Sales}: java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaProvider uri content://media/external/images/media from pid=2525, uid=10347 requires android.permission.READ_EXTERNAL_STORAGE, or grantUriPermission()
                  at android.app.ActivityThread.deliverResults(ActivityThread.java:4925)
                  at android.app.ActivityThread.handleSendResult(ActivityThread.java:4968)
                  at android.app.ActivityThread.access$1600(ActivityThread.java:222)
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1849)
                  at android.os.Handler.dispatchMessage(Handler.java:102)
                  at android.os.Looper.loop(Looper.java:158)
                  at android.app.ActivityThread.main(ActivityThread.java:7229)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
               Caused by: java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaProvider uri content://media/external/images/media from pid=2525, uid=10347 requires android.permission.READ_EXTERNAL_STORAGE, or grantUriPermission()
                  at android.os.Parcel.readException(Parcel.java:1620)
                  at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:183)
                  at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:135)
                  at android.content.ContentProviderProxy.query(ContentProviderNative.java:421)
                  at android.content.ContentResolver.query(ContentResolver.java:502)
                  at android.content.ContentResolver.query(ContentResolver.java:445)
                  at com.kosalgeek.android.photoutil.RealPathUtil.getDataColumn(RealPathUtil.java:131)
                  at com.kosalgeek.android.photoutil.RealPathUtil.getRealPathFromURI_API19(RealPathUtil.java:62)
                  at com.kosalgeek.android.photoutil.GalleryPhoto.getPath(GalleryPhoto.java:49)
                  at com.example.jijoabraham.informe.Sales.onActivityResult(Sales.java:143)
                  at android.app.Activity.dispatchActivityResult(Activity.java:7137)
                  at android.app.ActivityThread.deliverResults(ActivityThread.java:4921)
                  at android.app.ActivityThread.handleSendResult(ActivityThread.java:4968) 
                  at android.app.ActivityThread.access$1600(ActivityThread.java:222) 
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1849) 
                  at android.os.Handler.dispatchMessage(Handler.java:102) 
                  at android.os.Looper.loop(Looper.java:158) 
                  at android.app.ActivityThread.main(ActivityThread.java:7229) 
                  at java.lang.reflect.Method.invoke(Native Method) 
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 

1 个答案:

答案 0 :(得分:0)

在Marshmallow及以上版本中,您需要在运行时检查权限。

您可以使用SimplePermissions之类的库来实现此目标