android将位图正确保存为jpeg但不在图库中显示

时间:2014-07-17 13:16:44

标签: android bitmap gallery

大家好我保存我的图像视图Bitmap为jpeg / png格式,但它们没有在我的图库中列出(android默认图库)

这是我的方法:

    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();
            }

        }

此方法适用于我并保存位图但它没有在我的图像中列出gellery我该怎么做才能克服这个问题?

2 个答案:

答案 0 :(得分:2)

你需要&#34;告诉&#34;您添加了图片的图库。您可以通过MediaScannerConnection#scanFile

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

取自this answer。您可能希望查看该帖子中的其他答案,因为它们也可以解决您的问题。

答案 1 :(得分:0)

现在正在测试哪台设备? 使用真实设备并对其进行测试。 或者如果您正在使用Genymotion,那么您必须重新启动该设备,然后再次打开它并检查gallary。(它的Genymotion问题)。