在图库android中保存图像

时间:2015-07-04 05:04:51

标签: android gallery

我正在使用此代码保存图片:

URL url = null;
            try {
                url = new URL("image");
            } catch (MalformedURLException e1) {
                e1.printStackTrace();
            }
            Bitmap bmp = null;
            try {
                bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
            } catch (IOException e1) {
                e1.printStackTrace();
            }
            OutputStream output;
            File filepath = Environment.getExternalStorageDirectory();
            File dir = new File(filepath.getAbsolutePath() + "/folder name/");
            dir.mkdirs();
            File file = new File(dir, image + ".png");
            Toast.makeText(HomeActivity.this, "Image Saved to SD Card", Toast.LENGTH_SHORT).show();
            try {

                output = new FileOutputStream(file);
                bmp.compress(Bitmap.CompressFormat.PNG, 100, output);
                output.flush();
                output.close();

            }

            catch (Exception e) {
                e.printStackTrace();
            }

问题是当此代码在棒棒糖设备上运行时,图片未显示在图库中。我必须安装文件管理器来检查这些图像。

使用此代码:

    MediaStore.Images.Media.insertImage(getContentResolver(), bmp, "image";

图像保存在相机文件夹中。

我想在所有Android设备中使用特定文件夹名称在图库中显示图像。

请帮忙。

5 个答案:

答案 0 :(得分:13)

public void saveImageToExternal(String imgName, Bitmap bm) throws IOException {
    //Create Path to save Image
    File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES+appFolder); //Creates app specific folder
    path.mkdirs();
    File imageFile = new File(path, imgName+".png"); // Imagename.png
    FileOutputStream out = new FileOutputStream(imageFile);
    try{
        bm.compress(Bitmap.CompressFormat.PNG, 100, out); // Compress Image
        out.flush();
        out.close();

        // Tell the media scanner about the new file so that it is
        // immediately available to the user.
        MediaScannerConnection.scanFile(context,new String[] { imageFile.getAbsolutePath() }, null,new MediaScannerConnection.OnScanCompletedListener() {
            public void onScanCompleted(String path, Uri uri) {
                 Log.i("ExternalStorage", "Scanned " + path + ":");
                 Log.i("ExternalStorage", "-> uri=" + uri);
            }
        });
    } catch(Exception e) {
        throw new IOException();
    }
}

为我工作。 谢谢你的时间

答案 1 :(得分:1)

只是你的代码缺少MediaScannerConnection类。此类扫描从您的应用程序创建的库中的新文件和目录。查看演示此内容的完整演示示例。 http://whats-online.info/science-and-tutorials/135/how-to-save-an-image-to-gallery-in-android-programmatically/

答案 2 :(得分:0)

只需更改这些行,就可以了 -

File filepath = Environment.getExternalStorageDirectory().toString();
        File dir = new File(filepath + "/folder name/");
        dir.mkdirs();
        File file = new File(dir, image + ".png");

答案 3 :(得分:0)

如果你想在目录中保存一个图像,那么这个代码对我有用!

saveImage(data);

            private void saveImage(Bitmap data) {
                File createFolder = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),"test");
                if(!createFolder.exists())
                createFolder.mkdir();
                File saveImage = new File(createFolder,"downloadimage.jpg");
                try {
                    OutputStream outputStream = new FileOutputStream(saveImage);
                    data.compress(Bitmap.CompressFormat.JPEG,100,outputStream);
                    outputStream.flush();
                    outputStream.close();
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

答案 4 :(得分:0)

这对我有用。

添加MediaScannerConnection来扫描文件。指定文件网址 mimeType

MediaScannerConnection.scanFile(context,new String[] { image.getAbsolutePath() }, 
new String[] {"images/*"}, new MediaScannerConnection.OnScanCompletedListener() {
    public void onScanCompleted(String path, Uri uri) {
        Log.i("ScanCompleted", "Scanned " + path + ":");
    }
});