Search code examples
androidviewscale

How to scale and save View to SDCard


I have one View now I am trying to save it as fix size for example 500*666 into sdcard. Image save successfully but Image save as following. Image is save as on corner of entire image Please see attached image.

Code :

 private void onSave(final View v) {
 v.setMinimumWidth(500);
        v.setMinimumHeight(666);
Bitmap viewBitmap_save = null;

                            savingdialog();
                            DisplayMetrics dm = new DisplayMetrics();
                            getWindowManager().getDefaultDisplay().getMetrics(
                                    dm);
                            Display display = getWindowManager()
                                    .getDefaultDisplay();

                            viewBitmap_save = Bitmap.createBitmap(500, 666,
                                    Bitmap.Config.ARGB_8888);// i
                            Canvas canvas = new Canvas(viewBitmap_save);
                            v.draw(canvas);
                            try {
                                Random randomGenerator = new Random();
                                long RANDOMFILENUMBER = randomGenerator
                                        .nextLong();
                                File wallpaperDirectory = new File(
                                        "/sdcard/Image Telling/");
                                // have the object build the directory
                                // structure, if needed.
                                wallpaperDirectory.mkdirs();
                                // create a File object for the output file

                                // FileOutputStream outStream = new
                                // FileOutputStream(file);
                                File outputFile = new File(wallpaperDirectory,
                                        RANDOMFILENUMBER + ".png");
                                FileOutputStream outStream = new FileOutputStream(
                                        outputFile);
                                viewBitmap_save.compress(CompressFormat.PNG,
                                        100, outStream);

                                sendBroadcast(new Intent(
                                        Intent.ACTION_MEDIA_MOUNTED,
                                        Uri.parse("file://"
                                                + Environment
                                                        .getExternalStorageDirectory())));

                            } catch (Exception e) {
                                // TODO: handle exception
                            }
}

I am giving width and hight as fix because i need image as fixed size. How to scale View ?? Please help me.

enter image description here


Solution

  • Just a suggestion.

    Do not give any height,width to view and then get Bitmap of Original View as you are getting.

    Once you got bitmap created from view, then apply scaling.. you may use Bitmap.createScaledBitmap()

    And then save that scaled bitmap.