android - 如何将图像从数据库导出到SD卡

标签 android export android-sdcard

这个方法帮助我将图像从数据库显示到imgview。 那么如何将其保存到SD卡上呢?

private Bitmap setImage(String base64String) {
    Bitmap bmp = null;
    try {
        if (base64String == null || base64String.equals("")) {


        } else {

            byte[] decodedString = Base64.decode(base64String, Base64.DEFAULT);

            bmp =  BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);

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

eclipse 安卓2.2

最佳答案

从您的方法中获取位图返回,您可以将该位图存储到SD卡中。

在android中我们可以通过类似的方式保存位图。

然后您必须从 SD 卡获取目录(文件对象),例如:

File deviceSdcardDirectory = Environment.getExternalStorageDirectory();

接下来,创建用于图像存储的特定文件:

File image = new File(deviceSdcardDirectory, "file_name.png");

之后,您只需编写位图:

// Encode the file as a PNG image.
FileOutputStream outStream;
try {

    outStream = new FileOutputStream(image);
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, outStream); 
    /* 100 to keep full quality of the image */

    outStream.flush();
    outStream.close();

} catch (FileNotFoundException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}

不要忘记在 list 文件中添加以下权限:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

关于android - 如何将图像从数据库导出到SD卡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22062776/

相关文章:

java - 如何在android开发中随机化if语句

c++ - 从 dll 中导出省略号函数

java - android - ProgressDialog 中的 CountDownTimer

android - 无法为亚马逊应用内购买请求商品数据

database - 如何使用 psql(没有 PGCOPY header )导出二进制文件?

android - 以 block 的形式保存位图图像

android - 将应用程序安装到 sd 卡(外部存储)

android - 未在 Android 模拟器(虚拟设备)上找到 SD 卡文件夹?

javascript - Android native 浏览器中的奇怪 JavaScript 行为

jquery - 使用 jQuery 和 HTML 导出为 CSV