简体   繁体   中英

Saving bitmap to SD card with out compressing it in android

I am using android inbuilt camera to take picture and then attaching the same picture to email, when i am testing this functionality in 1.6 device, i am able name the picture that to be taken by in built camera, but in 2.1, the picture is having a name ie given by device,

How to give user defined name in 2.1 inbuilt camera images..

to avoid that problem i am saving the image manually but when i try to get the image back through intent as bitmap and then saving it to sd card using compress method

this methods handles result from inbuilt camera

protected void onActivityResult(int requestCode, int resultCode, Intent data)
 {
  File file = new File(Environment.getExternalStorageDirectory()
    + "/test.png");
  switch (requestCode)
  {
  case PHOTO_ACTION:
   if (resultCode == RESULT_CANCELED)
   {
     addPhoto = false;
     Toast.makeText(this, "Canceled ", Toast.LENGTH_LONG).show();
     break;
   } else if (resultCode == RESULT_OK)
   {
    Bundle b = data.getExtras();
    Bitmap bm = (Bitmap) b.get("data");

    FileOutputStream out;
    try
     {

     out = new FileOutputStream(file);
     bm.compress(Bitmap.CompressFormat.JPEG, 100, out);
     out.flush();
     out.close();
     scanPhoto(file.toString());
     out = null;
     addPhoto = true;
     } catch (Exception e)
      {
       e.printStackTrace();
       addPhoto = false;
      }

but when i am storing like this i am getting two images. one with system given name and other with name given by me. but the image one which has user defined is having less resolution so i question is how to save the bitmap with more resolution with out compressing it .. please help.... me

If you just want to save the bitmap without losing quality try using CompressFormat.PNG instead of JPEG, I've seen people having that problem before. Try changing:

bm.compress(Bitmap.CompressFormat.JPEG, 100, out);

with:

bm.compress(Bitmap.CompressFormat.PNG, 100, out);

and see it it helps.

Apart from Rick answer above, make sure your are providing a URI to camera intent where it can save the full image, else it will return thumb image in data parameter of intent. So it will be like:

Intent photoPickerIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
imgFile = new File("Cache directory","img.png"); //== where you want full size image
photoPickerIntent.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(imgFile));
startActivityForResult(photoPickerIntent, PickPhoto);

This was the error that I was doing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM