Camera Intent doesn't save image to gallery

Ucha Uswatun Hasanah :

I'm a newbie android developer.Now I found an error when I compiled my camera application.

Actually the camera intent is working properly and the image taken can be shown on ImageView, but it isn't saved automatically to my phone gallery. After research, it took 3 days after picture taken to get saved in the image gallery.

I already tried some different methods of camera intent, but until now I have no significant results. Any help will be so much appreciated, thank you.

my target SDK is 27 API, I already made an XML file for provider_paths and describe it in my manifests.

here is my camera intent:

public static final int REQUEST_CAMERA = 100;
Uri fileUri;
public final int SELECT_FILE = 11;

int bitmap_size = 40; // image quality 1 - 100;
int max_resolution_image = 800;

private void openCamera() {
    Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    fileUri = getOutputMediaFileUri();
    intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
    startActivityForResult(intent, REQUEST_CAMERA);
}

public Uri getOutputMediaFileUri() {
    return FileProvider.getUriForFile(LaporActivity.this,
            BuildConfig.APPLICATION_ID + ".fileprovider",
            getOutputMediaFile());
}

private static File getOutputMediaFile() {

    // External sdcard location
    File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "KSD");

    // Create the storage directory if it does not exist
    if (!mediaStorageDir.exists()) {
        if (!mediaStorageDir.mkdirs()) {
            Log.e("Monitoring", "Oops! Failed create Monitoring directory");
            return null;
        }
    }

    // Create a media file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(new Date());
    File mediaFile;
    mediaFile = new File(mediaStorageDir.getPath() + File.separator + "IMG_KSD_" + timeStamp + ".jpg");

    return mediaFile;
}

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    Log.e("onActivityResult", "requestCode " + requestCode + ", resultCode " + resultCode);

    if (resultCode == Activity.RESULT_OK) {
        if (requestCode == REQUEST_CAMERA) {
            try {
                Log.e("CAMERA", fileUri.getPath());

                bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(fileUri));
                setToImageView(getResizedBitmap(bitmap, max_resolution_image));
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else if (requestCode == SELECT_FILE && data != null && data.getData() != null) {
            try {
                //  choose picture from Gallery
                bitmap = MediaStore.Images.Media.getBitmap(LaporActivity.this.getContentResolver(), data.getData());
                setToImageView(getResizedBitmap(bitmap, max_resolution_image));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

private void setToImageView(Bitmap bmp) {
    //compress image
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    bmp.compress(Bitmap.CompressFormat.JPEG, bitmap_size, bytes);
    decoded = BitmapFactory.decodeStream(new ByteArrayInputStream(bytes.toByteArray()));

    //shows chosen picture from gallery to imageview
    fotoCaptured.setImageBitmap(decoded);
}

public Bitmap getResizedBitmap(Bitmap image, int maxSize) {
    int width = image.getWidth();
    int height = image.getHeight();

    float bitmapRatio = (float) width / (float) height;
    if (bitmapRatio > 1) {
        width = maxSize;
        height = (int) (width / bitmapRatio);
    } else {
        height = maxSize;
        width = (int) (height * bitmapRatio);
    }
    return Bitmap.createScaledBitmap(image, width, height, true);
}

I expected the picture to get saved automatically to my gallery so I can continue to upload-process method using Retrofit, but the actual is when I clicked upload-button, the toast shows "IMGxxxxx No such file/directory" because the image path is not properly saved.

Giddy Naya :

Your file exist in memory but not on disk. Create the file before returning the object

private static File getOutputMediaFile() { 
   ...
   ...
   //create file on disk
   if(!mediaFile.exists()) mediaFile.createNewFile();

   return mediaFile;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=162204&siteId=1