Skip to content Skip to sidebar Skip to footer

Save Scaled Image To Sdcard Android

i've the following code, that scale the object. Now i want to save the scaled image to sdcard, please help me regarding that i've searched many but didn't find any public boolean

Solution 1:

You could do something like this:

publicvoidsaveAsJpeg(View view, File file) {
    view.setDrawingCacheEnabled(true);
    view.measure(
        MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
        MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)
    );

    view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
    view.setVisibility(View.VISIBLE);

    Bitmap_b= Bitmap.createBitmap(
        view.getMeasuredWidth(),
        view.getMeasuredHeight(),
        Bitmap.Config.ARGB_8888
    );

    Canvas_c=newCanvas(_b);
    view.draw(_c);

    OutputStream_out=null;
    try {
        _out = newFileOutputStream(file);
        _b.compress(Bitmap.CompressFormat.JPEG, 100, _out);             
    } finally {
        _out.close();
    }
}

The above is heavily modified from some working code I have which produces bitmaps by drawing off-screen. I have not tested it in the form it appears above, and for your needs it may be doing more work than is really necessary.

It occurs to me that the following, shorter version, might work for you, but I have not tested this at all:

publicvoidsaveAsJpeg(View view, File file) {
    view.setDrawingCacheEnabled(true);
    Bitmap_b= view.getDrawingCache();
    OutputStream_out=null;
    try {
        _out = newFileOutputStream(file);
        _b.compress(Bitmap.CompressFormat.JPEG, 100, _out);             
    } finally {
        _out.close();
    }
}

Solution 2:

You can do it, this way

privatevoidsaveImage(ImageView view, File file) throws IOException{
        //Before saving image, you need to create a scaled bitmap object //which reflects the actual scaling, rotation, zoom, etc. in itself
          Bitmap src=((BitmapDrawable)view.getDrawable()).getBitmap();
          Bitmap bm= Bitmap.createBitmap(src, 0,0, src.getWidth(), src.getHeight(), matrix, true);

        //Then save this bitmap in a particular file.
          OutputStream out = null;
          try {
            out = new FileOutputStream(file);
            bm.compress(Bitmap.CompressFormat.PNG, 100, out);             
          } catch (FileNotFoundException e) {
            e.printStackTrace();
          } finally {
            out.close();
          }
    }

This is working code!

Post a Comment for "Save Scaled Image To Sdcard Android"