Android
Android

Reputation: 9033

android saving bitmap as jpeg properly but not display in image gallery

Hello all i am saving my imageviews Bitmap as jpeg/png format but they are not listing in my image gallery( android default gallery)

THis is my method:

    public void save_Image() {

            String path = Environment.getExternalStorageDirectory().toString()
                    + "/ImageFrame";
            Log.d("Files", "Path: " + path);

            File f = new File(path);

            File file[] = f.listFiles();

            Log.d("Files", "Size: " + file.length);
            for (int i = 0; i < file.length; i++) {
                Log.d("Files", "FileName:" + file[i].getName());
            }

            // BitmapDrawable drawable = (BitmapDrawable) img_large.getDrawable();
            // Bitmap bitmap = drawable.getBitmap();

            RelativeLayout frm_l = (RelativeLayout) findViewById(R.id.frm_l);

            frm_l.setDrawingCacheEnabled(true);
            Bitmap bitmap = frm_l.getDrawingCache();

            File image = new File(sdCardDirectory + "/ImageFrame/", "HDWallpaper "
                    + file.length + ".png");

    //File image = new File(sdCardDirectory + "/ImageFrame/", "HDWallpaper "
            //  + file.length + ".jpeg");
            boolean success = false;
            FileOutputStream outStream;
            try {

                outStream = new FileOutputStream(image);
                bitmap.compress(Bitmap.CompressFormat.PNG, 100, outStream);
//          bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outStream);

                /* 100 to keep full quality of the image */

                outStream.flush();
                outStream.close();
                success = true;
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

            if (success) {
                System.out.println("this is path where stored.."
                        + Environment.getExternalStorageDirectory()
                                .getAbsolutePath());
                Toast.makeText(getApplicationContext(),

                "Saved Successfully", Toast.LENGTH_LONG).show();
            } else {
                Toast.makeText(getApplicationContext(),
                        "Error during image sending", Toast.LENGTH_LONG).show();
            }

        }

This method works properly for me and saves bitmap But it does not list in my image gellery what should i do to overcome this problem?

Upvotes: 1

Views: 891

Answers (2)

Manuel Allenspach
Manuel Allenspach

Reputation: 12745

You need to "tell" the gallery that you added an image. You can do this via MediaScannerConnection#scanFile:

MediaScannerConnection.scanFile(this,
    new String[] { "YourFilePathHere" }, null,
    new MediaScannerConnection.OnScanCompletedListener() {
        public void onScanCompleted(String path, Uri uri) {
            //now visible in gallery
        }
    }
);

taken from this answer. You may want to look at the other answers from that post, since they also solve your problem.

Upvotes: 2

Harsh Patel
Harsh Patel

Reputation: 1086

On which device u r testing right now ? Use real device and test on it. Or if you are using Genymotion then you must have to restart that device and then Open it again and check in gallary.(It's Genymotion problem).

Upvotes: 0

Related Questions