如何在Android库中保存图像

Gat*_*o06 21 android android-imageview

我尝试将图像保存到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();
        }
Run Code Online (Sandbox Code Playgroud)

Tal*_*nel 69

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

这是一个常见的错误.

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

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

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);
}
Run Code Online (Sandbox Code Playgroud)


Ede*_*der 8

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

MediaStore.Images.Media.insertImage(getContentResolver(), yourBitmap, yourTitle , yourDescription);
Run Code Online (Sandbox Code Playgroud)

该代码将在Gallery的末尾添加图像.所以,请检查你的画廊图片,确定


Roh*_*ude 5

尝试添加此:

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

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