I was working on image handling techniques last week. All I wanted to upload and download the images for my app. There were situations where I can easily handle the uploading and downloading of the images. But then there were also some situations when this technique wasn’t working at all. Well, to be specific, when I was taking images from gallery then it was working very fine which was not the case with images captured by Camera.
In some cases, such as selecting images from Gallery, the below code works fine.
Uri uri_gallery = data.getData();
StorageReference filepath = “your firebase location path”
filepath.putFile(uri_gallery).addOnSuccessListener
(new OnSuccessListener<UploadTask.TaskSnapshot>()
{
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot)
{
String downloadUriGallery = taskSnapshot.getDownloadUrl().toString();
Uri downloadUri = taskSnapshot.getDownloadUrl();
Picasso.with(ChatRoomActivity.this).load(downloadUriGallery).into(imageView);
}
}
But for capturing image from camera needed a different approach. Then I learned about use of bitmap.
if(data.getData()==null)dd
{
bitmap = (Bitmap)data.getExtras().get("data");
}
else
{
try
{
bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), data.getData());
}
catch (IOException e)
{
e.printStackTrace();
}
}
Uri uri_camera = getImageUri(getApplicationContext(),bitmap);
StorageReference filepath =”your Firebase storage path” ;
filepath.putFile(uri_camera).addOnSuccessListener
(new OnSuccessListener<UploadTask.TaskSnapshot>()
{
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot)
{
String downloadCamera = taskSnapshot.getDownloadUrl().toString();
}
}
public Uri getImageUri(Context inContext, Bitmap inImage)
{
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
String path = MediaStore.Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
return Uri.parse(path);
}
So Finally, I was able to handle images taken from Camera as well for my app. You can always leave a comment below if you too have issues related to handling the images for your project. I will try to fix that issues with best of my knowledge.