0

I've simple app for capturing image using Camera using following code

@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);
}

now I want to upload that image to Firebase storage

@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]
                }
            })
            );
}

in uploadFromUri() at line

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

image is not set in ImageView and I get error

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)

and if I open this link I see image there, question is why it is not set in image view

Arshad Ali
  • 3,082
  • 12
  • 56
  • 99

2 Answers2

4

setImageURI() is for content URIs particular to the Android platform, not URIs specifying Internet resources.

Try getting your bitmap from internet in a new thread an then add it to your ImageView. Like this:

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;
    } 

You also can use a useful library to set image (Internal and external images) called Picasso http://square.github.io/picasso/

Miguel Benitez
  • 2,322
  • 10
  • 22
0

Add Picasso library for image loading and use the following code.

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");
        }
});
Vora Ankit
  • 682
  • 5
  • 8