设置图像uri会导致无法解码流:java.io.FileNotFoundException:

设置图像uri会导致无法解码流:java.io.FileNotFoundException:,java,android,firebase,firebase-storage,Java,Android,Firebase,Firebase Storage,我有一个简单的应用程序,使用以下代码使用相机拍摄图像 @AfterPermissionGranted(RC_STORAGE_PERMS) private void launchCamera() { Log.d(TAG, "launchCamera"); // Check that we have permission to read images from external storage. String perm = android.Manifest.permissi

我有一个简单的应用程序,使用以下代码使用相机拍摄图像

@AfterPermissionGranted(RC_STORAGE_PERMS)
private void launchCamera() {
    Log.d(TAG, "launchCamera");

    // Check that we have permission to read images from external storage.
    String perm = android.Manifest.permission.READ_EXTERNAL_STORAGE;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M
            && !EasyPermissions.hasPermissions(this, perm)) {
        EasyPermissions.requestPermissions(this, getString(R.string.rationale_storage),
                RC_STORAGE_PERMS, perm);
        return;
    }

    // Create intent
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

    // Choose file storage location
    File file = new File(Environment.getExternalStorageDirectory(), UUID.randomUUID().toString() + ".jpg");
    mFileUri = Uri.fromFile(file);
    takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, mFileUri);

    // Launch intent
    startActivityForResult(takePictureIntent, RC_TAKE_PICTURE);
}
现在我想将该图像上载到Firebase存储

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    Log.d(TAG, "onActivityResult:" + requestCode + ":" + resultCode + ":" + data);
    if (requestCode == RC_TAKE_PICTURE) {
        if (resultCode == RESULT_OK) {
            if (mFileUri != null) {
                uploadFromUri(mFileUri);
            } else {
                Log.w(TAG, "File URI is null");
            }
        } else {
            Toast.makeText(this, "Taking picture failed.", Toast.LENGTH_SHORT).show();
        }
    }
}

private void uploadFromUri(Uri fileUri) {
    Log.d(TAG, "uploadFromUri:src:" + fileUri.toString());

    // [START get_child_ref]
    // Get a reference to store file at photos/<FILENAME>.jpg
    final StorageReference photoRef = mStorageRef.child("photos")
            .child(fileUri.getLastPathSegment());
    // [END get_child_ref]

    // Upload file to Firebase Storage
    // [START_EXCLUDE]
    showProgressDialog();
    // [END_EXCLUDE]
    Log.d(TAG, "uploadFromUri:dst:" + photoRef.getPath());
    photoRef.putFile(fileUri)
            .addOnSuccessListener(this, new OnSuccessListener<UploadTask.TaskSnapshot>() {
                @Override
                public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                    // Upload succeeded
                    Log.d(TAG, "uploadFromUri:onSuccess");

                    // Get the public download URL
                    mDownloadUrl = taskSnapshot.getMetadata().getDownloadUrl();
                    Log.w("IMAGE_URL", "Path is " + mDownloadUrl.toString());
                    uploadedImage = (ImageView) findViewById(R.id.uploaded_img);

                    try{// Here I'm setting image in ImageView                            
                        uploadedImage.setImageURI(mDownloadUrl);
                    }catch (Exception e){
                        System.out.print(e.getCause());
                    }

                    // [START_EXCLUDE]
                    hideProgressDialog();
                    ///updateUI(mAuth.getCurrentUser());
                    // [END_EXCLUDE]
                }
            })
            );
}
图像未在ImageView中设置,我收到错误消息

07-29 09:54:23.055 18445-18445/? W/IMAGE_URL: Path is https://firebasestorage.googleapis.com/v0/b/connectin-a74da.appspot.com/o/photos%2F7dd3d46f-ed7b-4020-bc89-fd9e19a8ec65.jpg?alt=media&token=5b4f9ad7-1e99-42b8-966d-50c74fc2eab6
07-29 09:54:23.056 18445-18445/? E/BitmapFactory: Unable to decode stream: java.io.FileNotFoundException: https:/firebasestorage.googleapis.com/v0/b/connectin-a74da.appspot.com/o/photos%2F7dd3d46f-ed7b-4020-bc89-fd9e19a8ec65.jpg?alt=media&token=5b4f9ad7-1e99-42b8-966d-50c74fc2eab6: open failed: ENOENT (No such file or directory)
如果我打开这个链接,我会在那里看到图像,问题是为什么不在图像视图中设置它 平台,而不是指定Internet资源的URI

尝试在新线程中从internet获取位图,然后将其添加到ImageView。像这样:

uploadedImage.setImageBitmap(getImageBitmap(mDownloadUrl));


private Bitmap getImageBitmap(String url) {
        Bitmap bm = null;
        try {
            URL aURL = new URL(url);
            URLConnection conn = aURL.openConnection();
            conn.connect();
            InputStream is = conn.getInputStream();
            BufferedInputStream bis = new BufferedInputStream(is);
            bm = BitmapFactory.decodeStream(bis);
            bis.close();
            is.close();
       } catch (IOException e) {
           Log.e(TAG, "Error getting bitmap", e);
       }
       return bm;
    } 
您还可以使用一个有用的库来设置名为Picasso的图像(内部和外部图像)

setImageURI()
用于Android特定的内容URI 平台,而不是指定Internet资源的URI

尝试在新线程中从internet获取位图,然后将其添加到ImageView。像这样:

uploadedImage.setImageBitmap(getImageBitmap(mDownloadUrl));


private Bitmap getImageBitmap(String url) {
        Bitmap bm = null;
        try {
            URL aURL = new URL(url);
            URLConnection conn = aURL.openConnection();
            conn.connect();
            InputStream is = conn.getInputStream();
            BufferedInputStream bis = new BufferedInputStream(is);
            bm = BitmapFactory.decodeStream(bis);
            bis.close();
            is.close();
       } catch (IOException e) {
           Log.e(TAG, "Error getting bitmap", e);
       }
       return bm;
    } 

您还可以使用一个有用的库来设置名为Picasso的图像(内部和外部图像)

添加用于图像加载的Picasso库,并使用以下代码

Picasso.with(activity).load(imageURL)
    .resize(imageWidth,imageHeight)
    .into(imageView, new Callback() {
        @Override
        public void onSuccess() {
            Log.d(TAG,"successfully load the image");
        }

        @Override
        public void onError() {
            Log.d(TAG,"fail to load the image");
        }
});

添加用于图像加载的毕加索库,并使用以下代码

Picasso.with(activity).load(imageURL)
    .resize(imageWidth,imageHeight)
    .into(imageView, new Callback() {
        @Override
        public void onSuccess() {
            Log.d(TAG,"successfully load the image");
        }

        @Override
        public void onError() {
            Log.d(TAG,"fail to load the image");
        }
});