0

I have this code to upload an image from the gallery or user can use their camera to take the picture, but the result code for the camera returns -1 and crashes, I honestly don't know why. Here is the code:

private Button mSelectImage;
    private Button upload;
    private ImageView mImage;
    private StorageReference mStorage;
    private static final int GALLERY_INTENT = 2;
    private static final int CAMERA_REQUEST_CODE = 7;
    private ProgressDialog progressDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mStorage = FirebaseStorage.getInstance().getReference();
        mSelectImage = (Button) findViewById(R.id.selectImage);
        upload = (Button) findViewById(R.id.captureUpload);
        progressDialog = new ProgressDialog(this);
        mImage = (ImageView) findViewById(R.id.image);

        mSelectImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent i = new Intent(Intent.ACTION_PICK);
                i.setType("image/*");

                startActivityForResult(i, GALLERY_INTENT);
            }
        });

        upload.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                startActivityForResult(intent, CAMERA_REQUEST_CODE);



            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent 
    data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == GALLERY_INTENT && resultCode == RESULT_OK)
        {
            progressDialog.setMessage("uploading...");
            progressDialog.show();
            Uri  uri = data.getData();
            StorageReference filePath = 
   mStorage.child("Photos").child(uri.getLastPathSegment());

            filePath.putFile(uri).addOnSuccessListener(new 
  OnSuccessListener<UploadTask.TaskSnapshot>() {
                @Override
                public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) 
   {
                    Toast.makeText(MainActivity.this, "Image upload 
     successful",Toast.LENGTH_LONG).show();
                    progressDialog.dismiss();
                }
            }).addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {

                }
            });



        }
        else if(requestCode == CAMERA_REQUEST_CODE && resultCode == 
      RESULT_OK)
        {
            progressDialog.setMessage("uploading...");
            progressDialog.show();
            Uri  uri = data.getData();
            StorageReference filePath = 
    mStorage.child("Photos").child(uri.getLastPathSegment());
            filePath.putFile(uri).addOnSuccessListener(new 
      OnSuccessListener<UploadTask.TaskSnapshot>() {
                @Override
                public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) 
   {
                    Toast.makeText(MainActivity.this, "Image upload 
        successful",Toast.LENGTH_LONG).show();
                    progressDialog.dismiss();

                    Uri downloadUri = taskSnapshot.getDownloadUrl();







      Picasso.with(MainActivity.this)
       .load(downloadUri).fit().centerCrop().into(mImage);
                }
            }).addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {

                }
            });
        }
    }
 }

It works when I upload from the gallery but returns a -1 request result on the camera, just after I take the camera. It doesn't even go to the download part, (with Picasso)

Your help would be appreciated very much!!!! Best regards

java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=7, result=-1, data=Intent { act=inline-data (has extras) }} to activity {com.example.user.uploadingimages/com.example.user.uploadingimages.MainActivity}: java.lang.IllegalArgumentException: uri cannot be null

krumpking
  • 27
  • 1
  • 10

2 Answers2

0

Without a logcat i'd say it could be a permission issue:

<uses-permission android:name="android.permission.CAMERA"></uses-permission>

If you post the logcat i can be more precise

EDIT:

The logcat clearly states the problem

java.lang.IllegalArgumentException: uri cannot be null

Did you properly check that this statements returns a not null value?

Uri  uri = data.getData();
Fabio
  • 782
  • 4
  • 8
  • 25
0

As per logs, data.getData() is returning null.

Supply EXTRA_OUTPUT in your ACTION_IMAGE_CAPTURE Intent, in which case you know where the image should be stored, and do not need to rely upon getData()

On click of camera button, use this code:

  Intent intent = new Intent(
                        MediaStore.ACTION_IMAGE_CAPTURE);
                File f = new File(android.os.Environment
                        .getExternalStorageDirectory(), "temp.jpg");
                intent.putExtra(MediaStore.EXTRA_OUTPUT,
                        Uri.fromFile(f));

                 startActivityForResult(intent,
                        CAMERA_REQUEST_CODE);

Refer: Android data.getData() returns null from CameraActivity for some phones

seema
  • 991
  • 1
  • 13
  • 27