如何在android画廊中保存图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20859584/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 03:36:32  来源:igfitidea点击:

How to save image in android gallery

androidandroid-imageview

提问by Gatiko06

I try to save the image into WathsappIMG but when I go to image gallery android I don't see the image and the image there into the directory can be seen from ES File Explorer

我尝试将图像保存到 WathsappIMG 中,但是当我转到 android 图像库时,我看不到图像,并且可以从 ES 文件资源管理器中看到目录中的图像

OutputStream output;
       // Find the SD Card path
        File filepath = Environment.getExternalStorageDirectory();

      // Create a new folder in SD Card
     File dir = new File(filepath.getAbsolutePath()
              + "/WhatSappIMG/");
        dir.mkdirs(); 

     // Retrieve the image from the res folder
        BitmapDrawable drawable = (BitmapDrawable) principal.getDrawable();
        Bitmap bitmap1 = drawable.getBitmap();

        // Create a name for the saved image
        File file = new File(dir, "Wallpaper.jpg" );

        try {

            output = new FileOutputStream(file);

            // Compress into png format image from 0% - 100%
            bitmap1.compress(Bitmap.CompressFormat.JPEG, 100, output);
            output.flush();
            output.close();

        }

        catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

回答by Tal Kanel

the gallery don't displaying (necessarily) files from external storage.

图库不显示(必须)来自外部存储的文件。

this is a common mistake.

这是一个常见的错误。

the gallery displays images stored on the media store provider

图库显示存储在媒体商店提供商中的图像

you can use this method to store image file on media store provider:

您可以使用此方法在媒体商店提供商上存储图像文件:

public static void addImageToGallery(final String filePath, final Context context) {

    ContentValues values = new ContentValues();

    values.put(Images.Media.DATE_TAKEN, System.currentTimeMillis());
    values.put(Images.Media.MIME_TYPE, "image/jpeg");
    values.put(MediaStore.MediaColumns.DATA, filePath);

    context.getContentResolver().insert(Images.Media.EXTERNAL_CONTENT_URI, values);
}

回答by Edess Elder

here is what you should enter, when you're about to save the picture in the Gallery

这是您应该输入的内容,当您要在图库中保存图片时

MediaStore.Images.Media.insertImage(getContentResolver(), yourBitmap, yourTitle , yourDescription);

That code will add the image at the end of the Gallery. so please, check your Gallery picture, to be sure

该代码将在图库的末尾添加图像。所以请检查你的画廊图片,以确保

回答by Rohodude

Try adding this:

尝试添加这个:

MediaStore.Images.Media.insertImage(getContentResolver(), yourBitmap, yourTitle , yourDescription);

MediaStore.Images.Media.insertImage(getContentResolver(), yourBitmap, yourTitle , yourDescription);

Fill in your details for yourBitmap, yourTitle, and yourDescription, or just leave it as "".

填写 yourBitmap、yourTitle 和 yourDescription 的详细信息,或将其保留为"".

回答by Daniel Nyamasyo

You need to add a MediaScannerConnection class to your function of saving the image to the gallery. This class scans for new files and folders in gallery connected with your app. Add the following class to scan the newly saved image files or new added image directory to the gallery or download Source Code

您需要将 MediaScannerConnection 类添加到将图像保存到图库的功能中。此类扫描与您的应用程序连接的图库中的新文件和文件夹。添加以下类扫描新保存的图像文件或新添加的图像目录到图库或下载源代码

        MediaScannerConnection.scanFile(this, new String[]{file.toString()}, null,
                new MediaScannerConnection.OnScanCompletedListener() {
                    public void onScanCompleted(String path, Uri uri) {
                        Log.i("ExternalStorage", "Scanned " + path + ":");
                        Log.i("ExternalStorage", "-> uri=" + uri);
                    }
                });

Read more

阅读更多

回答by Denis Gordin

For Xamarin fellows:

对于 Xamarin 研究员:

public static void SaveToTheGalley(this string filePath, Context context)
{
    var values = new ContentValues();
    values.Put(MediaStore.Images.Media.InterfaceConsts.DateTaken, Java.Lang.JavaSystem.CurrentTimeMillis());
    values.Put(MediaStore.Images.Media.InterfaceConsts.MimeType, "image/jpeg");
    values.Put(MediaStore.MediaColumns.Data, filePath);
    context.ContentResolver.Insert(MediaStore.Images.Media.ExternalContentUri, values);
}

And don't forget about android.permission.WRITE_EXTERNAL_STORAGEpermission.

并且不要忘记android.permission.WRITE_EXTERNAL_STORAGE许可。

回答by Kanak Sony

You should change this piece of code-

您应该更改这段代码-

try {
        output = new FileOutputStream(file);

        // Compress into png format image from 0% - 100%
        bitmap1.compress(Bitmap.CompressFormat.JPEG, 100, output);
        output.flush();
        output.close();
        String url = Images.Media.insertImage(getContentResolver(), bitmap1,
        "Wallpaper.jpg", null);
    }

    catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }