0

Here is my code

This is the way I am compressing bitmap, but image quality totally gone.Please suggest answers to save image without losing quality.

Note: (Image was captured from camera)

public String saveImage(Bitmap myBitmap) {
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    myBitmap.compress(Bitmap.CompressFormat.PNG, 100, bytes);
    File wallpaperDirectory = new File(Environment.getExternalStorageDirectory() + "/"+Environment.DIRECTORY_PICTURES);

    Log.e("wallpaperDirectory","wallpaperDirectory"+wallpaperDirectory);
    if (!wallpaperDirectory.exists()) {  // have the object build the directory structure, if needed.
        wallpaperDirectory.mkdirs();
    }

    try {
        String timeStamp = new SimpleDateFormat("yyyyMMdd").format(new Date());
        File f = new File(wallpaperDirectory, "IMG_"+currentJob.getJob_number()+"_"+timeStamp+ ".png");
        f.createNewFile();
        FileOutputStream fo = new FileOutputStream(f);
        fo.write(bytes.toByteArray());
        MediaScannerConnection.scanFile(mContext,
                new String[]{f.getPath()},
                new String[]{"image/png"}, null);
        fo.close();
        Log.e("TAG", "File Saved::--->" + f.getAbsolutePath());

        return f.getAbsolutePath();
    } catch (IOException e1) {
        e1.printStackTrace();
        Log.e("hiii","hiiicrashh");
    }
    return "";
}
M.Neelima
  • 221
  • 1
  • 10
  • please suggest answers – M.Neelima Nov 20 '19 at 11:26
  • How exactly are you getting the `Bitmap` that you pass to that method? Something like `Bitmap bmp = (Bitmap) data.getExtras().get("data")` in `onActivityResult()`? – Mike M. Nov 20 '19 at 11:30
  • You are first compressing image to PNG format can you compress it to JPEG. JPEG is higher version of image. – Aditi Nov 20 '19 at 11:32
  • @MikeM. yes I am passing the bitmap value from onActivityResult() – M.Neelima Nov 20 '19 at 11:33
  • @Aditi I tried that too...but not worked – M.Neelima Nov 20 '19 at 11:34
  • 1
    OK, that's the actual problem, then. That `Bitmap` is only a thumbnail. A full resolution image can't be passed back on an `Intent`. You'll need to provide an `EXTRA_OUTPUT` on the `ACTION_IMAGE_CAPTURE` `Intent`. CommonsWare explains it well in the linked duplicates, and provides very good examples. You won't need your file save routine, then, btw. The camera app will already have saved the image to the file you gave it. – Mike M. Nov 20 '19 at 11:36
  • @MikeM. I want to capture Image from default camera without using any 3rd party..Is it possible to save exact image what I have captured from camera? – M.Neelima Nov 20 '19 at 11:44
  • 1
    You're still using the same camera app that you were before. You're just giving it a specific file to save the full resolution image to, in the form of a `Uri`. It has to be done that way, because a full resolution image won't fit on an `Intent`. There's really nothing third-party, here. You do, however, need to use `FileProvider` from the support library, because of restrictions on newer Android versions. I know, it's a few more steps, but if you want the full resolution image, that's what it takes. – Mike M. Nov 20 '19 at 11:48
  • @Mike I want image quality and the image size should be below 30kb...I tried the solution what you have told to me...I got the quality image but the size is 4 MB... – M.Neelima Nov 21 '19 at 05:36
  • That's not something that you will have control over, as far as the initial image save. That will be up to the user and the camera app. The best you can do is resize, or adjust the quality of, the image afterwards. – Mike M. Nov 21 '19 at 05:47

0 Answers0