4
testButton.setOnClickListener(new Button.OnClickListener(){

        @Override
        public void onClick(View v) 
        {
                    imageView.setImageBitmap(Bitmap);
                    imageView.buildDrawingCache();
                    Bitmap bm =imageView.getDrawingCache();

               Intent imageIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                File imagesFolder = new File(Environment.getExternalStorageDirectory(), "Punch");
                imagesFolder.mkdirs(); 
                String fileName = "image"  + ".jpg";
                File output = new File(imagesFolder, fileName);
                Uri uriSavedImage = Uri.fromFile(output);
                imageIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);
                OutputStream fos = null;

                try {
                    fos = getContentResolver().openOutputStream(uriSavedImage);
                    bm.compress(CompressFormat.JPEG, 100, fos);
                    fos.flush();
                    fos.close();
                    } 
                catch (FileNotFoundException e) 
                    {
                    e.printStackTrace();
                    } 
                catch (IOException e)
                {
                    e.printStackTrace();
                } 
                finally
                {}               
        }
        });

First i retrieved an image from sd card to image view of resolution (640 x 480). Then again i saved the image from image view to sd card. But the image saved is of resolution is (188x113). Can anyone suggest how i can save with the same resolution. Any suggestions will be appreciable.

Aswathy
  • 337
  • 2
  • 8
  • 18
  • You are saving the imageview as an image. Not the original image. Are you making some changes in the imageview that you want to save? – Aman Gautam Dec 04 '13 at 09:47
  • yes i am processing the retrieved image by removing its blue and green content. and making the image into a red color image.and then i am saving this red color image back to sd card. – Aswathy Dec 04 '13 at 09:51
  • if width and height of Bitmap is greater than width and height of ImageView, then ImageView will adjust Bitmap (actually not bitmap, it will convert Bitmap into BitmapDrawable) dimensions to fit to it's actual size. so you will get small size image – Gopal Gopi Dec 04 '13 at 10:04
  • i did it in another way....i saved the image right after i retrieved it in the image view i.e without any processing. still i found that the saved image is of less resolution..Any suggestions please – Aswathy Dec 04 '13 at 10:08
  • @Gopal i cannot change the height and width of image view coz a lot of things are placed in surrounding with it. Is there any possible way that when it save it save back to the sd card with the same resolution as that of the original one – Aswathy Dec 04 '13 at 10:13
  • @user3040168 then directly compress Bitmap – Gopal Gopi Dec 04 '13 at 10:19
  • @Gopal that wont work for me. I did something like i didnt use image view. i retrieved the image from sdcard to a bitmap and then processed the bitmap. now i need to save this processed bitmap back to sd file... – Aswathy Dec 04 '13 at 10:59
  • @user3040168 processing means? what you are doing on Bitmap? – Gopal Gopi Dec 04 '13 at 11:02
  • but your **bm** (Bitmap) id made from the drawing cache of ImageView. so it will be lesser in size – Gopal Gopi Dec 04 '13 at 11:14

3 Answers3

6

Try this code :

BitmapDrawable btmpDr = (BitmapDrawable) ivPic.getDrawable();
Bitmap bmp = btmpDr.getBitmap();

/*File sdCardDirectory = Environment.getExternalStorageDirectory();*/
try
{
    File sdCardDirectory = new File(Environment.getExternalStorageDirectory() + File.separator + "MeeguImages");
    sdCardDirectory.mkdirs();

    imageNameForSDCard = "image_" + String.valueOf(random.nextInt(1000)) + System.currentTimeMillis() + ".jpg";

    File image = new File(sdCardDirectory, imageNameForSDCard);
    FileOutputStream outStream;

    outStream = new FileOutputStream(image);
    bmp.compress(Bitmap.CompressFormat.JPEG, 100, outStream); 
    /* 100 to keep full quality of the image */
    outStream.flush();
    outStream.close();



    //Refreshing SD card
    sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"+ Environment.getExternalStorageDirectory())));
}
catch (Exception e) 
{
    e.printStackTrace();
    Toast.makeText(ViewImage.this, "Image could not be saved : Please ensure you have SD card installed " +
                                                                            "properly", Toast.LENGTH_LONG).show();
}
Aditya Gupta
  • 508
  • 3
  • 18
  • Android does not allow to broadcast `android.intent.action.MEDIA_MOUNTED`, [reference](http://stackoverflow.com/a/24072611/1708390) – Bugs Happen Oct 29 '15 at 11:26
0
   bm.compress(CompressFormat.JPEG, 100, fos);

remove this line

Rahul Gupta
  • 5,275
  • 8
  • 35
  • 66
0

Solved, This is how i achieved to save image from ImageView

/*Variable which holds Image*/
    {ImageView ivBanner = "Initialise It :)";
     FileOutputStream fileOutputStream = openFileOutput("ImageName" + ".jpg", MODE_PRIVATE);

     Bitmap bitmap = convertToBitMap(ivBanner.getDrawable(),ivBanner.getWidth(),ivBanner.getHeight());
     bitmap.compress(Bitmap.CompressFormat.JPEG, 85, fileOutputStream);
     File file = getFileStreamPath("ImageName" + ".jpg");
     File f = file.getAbsoluteFile();
     /*Utilise your path whatever way you want*/
     String localPath = f.getAbsolutePath();}

     /* Covert Drawable to Bitmap*/
    private Bitmap convertToBitMap(Drawable drawable, int width, int height) {
    Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    drawable.setBounds(0,0,width,height);
    drawable.draw(canvas);
    return bitmap;
}
Lokesh Tiwari
  • 10,496
  • 3
  • 36
  • 45