Reputation: 825
I'm trying to upload image to firebase storage after capturing it from phone camera but when I run my code, I get the above error. I have tried to check if the URI returned is null as shown in this line of code
Toast.makeText(UploadImage.this, "YOUR URI IS NULL", Toast.LENGTH_LONG).show();
but the Toast message does not get displayed, but error is thrown in the line following the toast which is
filepath = storageReference.child("PhotoModel").child(uri.getLastPathSegment());
Below is my whole code,
public class UploadImage extends AppCompatActivity {
private Button mUploadImg;
private ImageView mImageView;
private StorageReference storageReference, filepath;
private ProgressDialog progressDialog;
private static final int CAMERA_REQUEST_CODE = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_upload_image);
storageReference = FirebaseStorage.getInstance().getReference();
mUploadImg = (Button) findViewById(R.id.btnUploadImg);
mImageView = (ImageView) findViewById(R.id.imgView);
progressDialog = new ProgressDialog(this);
mUploadImg.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent Imgintent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(Imgintent, CAMERA_REQUEST_CODE);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CAMERA_REQUEST_CODE && resultCode == RESULT_OK) {
progressDialog.setMessage("Upload Image ...");
progressDialog.show();
Uri uri = data.getData();
if (uri == null) {
Toast.makeText(UploadImage.this, "YOUR URI IS NULL", Toast.LENGTH_LONG).show();
}
filepath = storageReference.child("PhotoModel").child(uri.getLastPathSegment());
filepath.putFile(uri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
progressDialog.dismiss();
Toast.makeText(UploadImage.this, "Uploading ...", Toast.LENGTH_LONG).show();
}
});
} else {
Toast.makeText(UploadImage.this, "Upload Failed...Check your Internet Connection", Toast.LENGTH_LONG).show();
}
}
}
Someone help me figure out where the problem starts, because I can't really put a finger on it right now...
Upvotes: 0
Views: 4542
Reputation: 825
Got a different way of implementing this functionality. Hope it helps someone.
First: Inside onCreate method. Assuming you already have your ImageButton(that will invoke camera) and upload button(to upload you image to firebase storage) defined in xml file, Initialize your ImageButton
/** Button for adding and Uploading image*/
mUploadImg = (ImageButton) findViewById(R.id.btnUploadImg);
Second: Set a onclick listener for the Imagebutton
/** Capture Image save and pass it to an intent */
mUploadImg.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent Imgintent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(Imgintent, CAMERA_REQUEST_CODE);
}
});
Third: Outside your onCreate method, implement startActivityForResult I have used Bitmap not Uri
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CAMERA_REQUEST_CODE && resultCode == RESULT_OK) {
//set the progress dialog
progressDialog.setMessage("Uploding image...");
progressDialog.show();
//get the camera image
Bundle extras = data.getExtras();
Bitmap bitmap = (Bitmap) data.getExtras().get("data");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] databaos = baos.toByteArray();
//set the image into imageview
mImageView.setImageBitmap(bitmap);
//String img = "fire"
//Firebase storage folder where you want to put the images
StorageReference storageRef = FirebaseStorage.getInstance().getReference();
//name of the image file (add time to have different files to avoid rewrite on the same file)
StorageReference imagesRef = storageRef.child(productname + new Date().getTime());
//send this name to database
//upload image
UploadTask uploadTask = imagesRef.putBytes(databaos);
uploadTask.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
Toast.makeText(UploadImage.this, "Sending failed", Toast.LENGTH_SHORT).show();
}
}).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
productDownloadUrl = taskSnapshot.getDownloadUrl().toString();
progressDialog.dismiss();
}
} );}
}
If this has helped you, remember to acknowledge my answer.
Upvotes: 1