在图库android中保存图像

问题描述 投票:4回答:5

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

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设备中显示具有特定文件夹名称的图库中的图像。

请帮忙。

android gallery
5个回答
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
投票

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


0
投票

只需更改这些行,它就会起作用 -

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

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

0
投票

这对我有用。

添加MediaScannerConnection以扫描文件。指定文件url和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 + ":");
    }
});
© www.soinside.com 2019 - 2024. All rights reserved.