-->

Receiving low quality image from camera intent [du

2019-08-30 01:45发布

问题:

This question already has an answer here:

  • Low picture/image quality when capture from camera 2 answers

I am trying to upload an image to firebase using the camera. But, the quality of image i get is too low (not readable). I have written the following code ( with no compilation error). But, can't understand why it's not able to produce a readable image.

I have tried varying the quality parameter of bitmap.compress but the image quality remains the same. But the same compression code works good for images coming from gallery.

uploadPp.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {


            AlertDialog.Builder builder;
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                builder = new AlertDialog.Builder(infoPp.this, android.R.style.Theme_Material_Dialog_NoActionBar_MinWidth);
            } else {
                builder = new AlertDialog.Builder(infoPp.this);
            }
            builder.setTitle("Picture Source")
                    .setMessage("Select source of picture")
                    .setPositiveButton("Camera", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {

                            Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                            if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
                                startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE_CNIC);
                            }

                        }
                    })
                    .setNegativeButton("Gallery", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {

                            Intent intent = new Intent();
                            intent.setType("image/*");
                            intent.setAction(Intent.ACTION_GET_CONTENT);
                            startActivityForResult(Intent.createChooser(intent, "Select Picture"), REQUEST_IMAGE_CAPTURE_CNIC);
                        }
                    })
                    .setIcon(android.R.drawable.ic_dialog_alert)
                    .show();


        }
    });


}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {

    mProgress.setMessage("Uploading.....");

    if (requestCode == REQUEST_IMAGE_CAPTURE_CNIC && resultCode == RESULT_OK) {

        Bitmap imageBitmap;

        try {

            Bundle extras = data.getExtras();
            imageBitmap = (Bitmap) extras.get("data");
            mProgress.show();

            encodeBitmapAndSaveToFirebase(imageBitmap,"ProfilePhoto");


        }catch(Exception e) {

            Uri file = data.getData();
            mProgress.show();
            encodeBitmapAndSaveToFirebase(file,"ProfilePhoto");


        }



    }

}


public void encodeBitmapAndSaveToFirebase(Bitmap bitmap,String fileName) { //////////// THISIS FOR CAMERA

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos); 
        path= "Verification Records/"+auth.getCurrentUser().getUid()+"/"+fileName+".jpeg";
        byte[] data=baos.toByteArray();
        StorageReference firememeRef = storage.getReference(path);
        UploadTask uploadTask= firememeRef.putBytes(data);
        ...

}


public void encodeBitmapAndSaveToFirebase(Uri file,String fileName){  /// THIS IS FOR GALLERY



    Uri selectedImage = file;

    InputStream imageStream = null;
    try {
        imageStream = getContentResolver().openInputStream(
                selectedImage);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

    Bitmap bmp = BitmapFactory.decodeStream(imageStream);

    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bmp.compress(Bitmap.CompressFormat.JPEG, 40, stream);
    String path= "Verification Records/"+auth.getCurrentUser().getUid()+"/"+fileName+".jpeg";
    byte[] data=stream.toByteArray();
    StorageReference firememeRef = storage.getReference(path);
    UploadTask uploadTask= firememeRef.putBytes(data);
    ...
}'

回答1:

The image provided in the data extra is only a thumbnail. To obtain the fullsize image you need to provide a fully qualified file name where the camera app should save the photo. This is described in the documentation, with example code provided.