Reputation: 644
I have a simple ImageView that when I click on it the camera appears and requests a photo, you take the photo and that ImageView turns into the photo, stuff like this:
Uri pictureUri;
ImageViewer pic = findViewById(...);
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_PIC_REQUEST) {
try {
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
String path=String.valueOf(data.getData());
pictureUri = Uri.fromFile(new File(path));
pic.setImageBitmap(thumbnail);
After the camera request the picture is loaded into the Imageviewer but and then I try to upload it to Firebase using:
StorageMetadata metadata = new StorageMetadata.Builder()
.setContentType(animal.getPictureID()+"/jpeg")
.build();
mStorageRef.child(animal.getPictureID()).putFile(pictureUri,metadata);
Where animal.getPictureID it's simply an ID given before. Now the issue here is that at the putFile() it keeps returning FileNotFoundException.
Also while we're at it if you know why the image in Imageviewer is in such low quality it would also be ncie but the main issue is really the Firebase storage.
Upvotes: 2
Views: 1879
Reputation: 1837
You can get Bitmap
from request by using:
InputStream stream = getContentResolver().openInputStream(data.getData());
Bitmap bitmap = BitmapFactory.decodeStream(stream);
To store it to Firebase storage:
String path = "images/"+imgName;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
// this will compress an image that the uplaod and download would be faster
bitmap.compress(Bitmap.CompressFormat.JPEG, 50, baos);
byte[] data = baos.toByteArray();
FirebaseStorage storage = FirebaseStorage.getInstance();
StorageReference storageReference = storage.getReference();
StorageReference reference = storageReference.child(path);
UploadTask uploadTask = reference.putBytes(data);
uploadTask.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
Log.i(TAG, "Image was saved");
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.e(TAG, "Image wasn't saved. Exception: "+e.getMessage());
}
});
Upvotes: 1