javaandroidbitmapandroid-galleryandroid-camera-intent

how to save high quality image to phone gallery by camera intent


I am trying to develop app which save image to gallery. I want to call the camera through intent, capture images, and save it locally in the gallery but the issue is image quality is very poor. can anyone help me figure out why?

if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
           
                Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                someActivityResultLauncher.launch(cameraIntent);}
 

    @Override
    public void onActivityResult(ActivityResult result) {
        if (result.getResultCode() == RESULT_OK) {
            bitmap = (Bitmap) Objects.requireNonNull(result.getData()).getExtras().get("data");
        }
        imageView.setImageBitmap(bitmap);     
        saveimage(bitmap);
     }
private void saveimage(Bitmap bitmap){
     Uri images;
     ContentResolver contentResolver = getContentResolver();

     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q){
         images = MediaStore.Images.Media.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY);
     }else {
         images = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
     }
     ContentValues contentValues = new ContentValues();
     contentValues.put(MediaStore.Images.Media.DISPLAY_NAME, System.currentTimeMillis() +".jpg");
     contentValues.put(MediaStore.Images.Media.MIME_TYPE, "images/*");
     Uri uri = contentResolver.insert(images, contentValues);
     try {
         OutputStream outputStream = contentResolver.openOutputStream(Objects.requireNonNull(uri));
         bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
         Objects.requireNonNull(outputStream);

        //

     }catch (Exception e){
        //
         e.printStackTrace();

     }
 }


Solution

  • cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri); if you get image from data directly, it's in bad quality