Firebase上传失败:提供结果ResultInfo

时间:2017-05-25 18:28:42

标签: android firebase firebase-realtime-database

我想通过相机对图像进行cpature,然后将其上传到firebase中。 相机工作正常。但不幸的是应用已经停止.. 我也允许上网和相机。 但异常仍然存在...... 这是我的onActivityResult方法......

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if(requestCode == CAMERA_REQUEST_CODE && resultCode == RESULT_OK){

        vProgress.setMessage("Uploading Image...");
        vProgress.show();

        Uri uri = data.getData();

        StorageReference filePath = dataReference.child("ProFilePics").child(uri.getLastPathSegment());

        filePath.putFile(uri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
            @Override
            public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                vProgress.dismiss();

                Toast.makeText(rentCarActivity.this, "uploaded...." , Toast.LENGTH_LONG).show();
            }
        });
    }
}

这是我的例外..请有人告诉我什么是布莱恩..

Failure delivering result ResultInfo{who=null, request=2, result=-1, data=Intent { act=inline-data dat=content://media/external/images/media/4928 typ=image/jpeg (has extras) }} to activity {com.mousetip.risith.rentcar/com.mousetip.risith.rentcar.rentCarActivity}: java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaProvider uri content://media/external/images/media/4928 from pid=20528, uid=10052 requires android.permission.READ_EXTERNAL_STORAGE, or grantUriPermission()
                                                                                at android.app.ActivityThread.deliverResults(ActivityThread.java:3786)
                                                                                at android.app.ActivityThread.handleSendResult(ActivityThread.java:3829)
                                                                                at android.app.ActivityThread.access$1400(ActivityThread.java:150)
                                                                                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1432)
                                                                                at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                at android.os.Looper.loop(Looper.java:148)
                                                                                at android.app.ActivityThread.main(ActivityThread.java:5517)
                                                                                at java.lang.reflect.Method.invoke(Native Method)
                                                                                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                                             Caused by: java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaProvider uri content://media/external/images/media/4928 from pid=20528, uid=10052 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.readExceptionWithFileNotFoundExceptionFromParcel(DatabaseUtils.java:146)
                                                                                at android.content.ContentProviderProxy.openTypedAssetFile(ContentProviderNative.java:692)
                                                                                at android.content.ContentResolver.openTypedAssetFileDescriptor(ContentResolver.java:1106)
                                                                                at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:944)
                                                                                at android.content.ContentResolver.openFileDescriptor(ContentResolver.java:797)
                                                                                at android.content.ContentResolver.openFileDescriptor(ContentResolver.java:751)
                                                                                at com.google.firebase.storage.UploadTask.<init>(Unknown Source)
                                                                                at com.google.firebase.storage.StorageReference.putFile(Unknown Source)
                                                                                at com.mousetip.risith.rentcar.rentCarActivity.onActivityResult(rentCarActivity.java:69)
                                                                                at android.app.Activity.dispatchActivityResult(Activity.java:6490)
                                                                                at android.app.ActivityThread.deliverResults(ActivityThread.java:3782)
                                                                                at android.app.ActivityThread.handleSendResult(ActivityThread.java:3829) 
                                                                                at android.app.ActivityThread.access$1400(ActivityThread.java:150) 
                                                                                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1432) 
                                                                                at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                at android.os.Looper.loop(Looper.java:148) 
                                                                                at android.app.ActivityThread.main(ActivityThread.java:5517) 
                                                                                at java.lang.reflect.Method.invoke(Native Method) 
                                                                                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                                                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

2 个答案:

答案 0 :(得分:0)

您需要明确要求获取读取外部存储空间的权限。只需添加这些方法并在尝试访问文件存储之前调用它们,它应该可以工作:

private static final int REQUEST_PERMISSION = 1;

private void requestReadFilePermission(){
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M
                && ContextCompat.checkSelfPermission(mView.getContext(), Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, REQUEST_PERMISSION);
        } else {
            //permission is already granted - do what you need
        }
    }

    @Override
    public void onRequestPermissionsResult(final int requestCode, @NonNull final String[] permissions, @NonNull final int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == REQUEST_PERMISSION) {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                //user granted permission - do what you need
            } else {
                Toast.makeText(mView.getContext(), "Need permission to read storage!", Toast.LENGTH_LONG).show();
            }
        }
    }

当然,您的AndroidManifest.xml文件中已声明<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

答案 1 :(得分:0)

你需要要求

android.permission.READ_EXTERNAL_STORAGE

通过运行时权限并在android 6.0及更高版本的清单中声明。在此之下,您可以在清单中声明该权限