Take image from SD card, compress it, and save it back to SD?
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 ima开发者_如何学编程ge file back into my apps internal memory?
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.
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
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.
精彩评论