1

Im trying to capture an image from the camera, and if its too big i want to compress the bitmap and send it back to the sd card. I initially tried to pull the image directly into internal memory but apparently according to this answer to my previous question i can do that: android picture from camera being taken at a really small size

How could I bring that image file back into my apps internal memory?

Community
  • 1
  • 1
jfisk
  • 6,125
  • 20
  • 77
  • 113

3 Answers3

0

You may rescale image using the code below.

Bitmap yourBitmap;
Bitmap resized = Bitmap.createScaledBitmap(yourBitmap, newWidth, newHeight, true);

This will compress your image to smaller size, you just have need to give new height and width as per your requirement.

nKn
  • 13,691
  • 9
  • 45
  • 62
0

Onclick of button Call Method to Save your Image to SDcard:

SaveImage(bitmap);

Saving Image to SdCard:

private void SaveImage(Bitmap finalBitmap) {

  String root = Environment.getExternalStorageDirectory().toString();
  File myDir = new File(root + "/demo_images");    
  myDir.mkdirs();
  Random generator = new Random();
  int n = 10000;
  n = generator.nextInt(n);
  fname = "Image-"+ n +".jpg";
  File file = new File (myDir, fname);
// Below code will give you full path in TextView

> txtPath1.setText(file.getAbsolutePath());

  if (file.exists ()) file.delete (); 
  try {
         FileOutputStream out = new FileOutputStream(file);
         finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
         out.flush();
         out.close();

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

For getting Image from SdCard:

Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
imageview.setImageDrawable(bitmap);

For Storing Image to Internal Memory:

Saving and Reading Bitmaps/Images from Internal memory in Android

Community
  • 1
  • 1
Sagar Shah
  • 4,272
  • 2
  • 25
  • 36
-2

The image that you get from the camera is already compressed in the jpeg format. You will never get a raw image from the camera.

bluefalcon
  • 4,225
  • 1
  • 32
  • 41