如何在android画廊中保存图像 [英] How to save image in android gallery

查看:45
本文介绍了如何在android画廊中保存图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

OutputStream 输出;//找到SD卡路径文件文件路径 = Environment.getExternalStorageDirectory();//在 SD 卡中创建一个新文件夹文件目录 = 新文件(filepath.getAbsolutePath()+ "/WhatSappIMG/");dir.mkdirs();//从 res 文件夹中检索图像BitmapDrawable drawable = (BitmapDrawable) principal.getDrawable();位图 bitmap1 = drawable.getBitmap();//为保存的图像创建一个名称File file = new File(dir, "Wallpaper.jpg" );尝试 {输出 = 新的 FileOutputStream(file);//从 0% - 100% 压缩成 png 格式的图像bitmap1.compress(Bitmap.CompressFormat.JPEG, 100, 输出);输出.flush();输出关闭();}捕获(异常 e){//TODO 自动生成的 catch 块e.printStackTrace();}

解决方案

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

这是一个常见的错误.

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

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

public static void addImageToGallery(final String filePath, final Context context) {ContentValues 值 = 新的 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);}

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

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

解决方案

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

这篇关于如何在android画廊中保存图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆