从相机意图接收低质量图像

时间:2018-04-13 16:49:40

标签: android camera

我正在尝试使用相机将图像上传到firebase。但是,我得到的图像质量太低(不可读)。我编写了以下代码(没有编译错误)。但是,无法理解为什么它无法生成可读的图像。

我尝试过改变bitmap.compress的质量参数,但图像质量保持不变。但是相同的压缩代码适用于来自图库的图像。

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 个答案:

答案 0 :(得分:2)

data extra中提供的图片为only a thumbnail。要获取完整大小的图像,您需要提供完全限定的文件名,相机应用程序应保存照片。这是described in the documentation,提供了示例代码。