将位图保存到应用程序文件夹 [英] Save bitmap to app folder

查看:129
本文介绍了将位图保存到应用程序文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用首次启动时,它使用户可以选择个人资料图片.现在可以通过拍照或从画廊中选择照片来完成.

When my app first starts, it lets the user select a profile picture. This can be done taking a photo at the moment, or selecting it from the gallery.

用户获取图片后,该图片必须保存在设备的内部存储中,并将在应用程序中用作用户的个人资料图片.

After the user gets a picture, this must be saved in the device's internal storage and will be used in the app as user's profile picture.

此过程运行良好,用户得到了图片,并且在保存之前将其显示在imageview中.但是要将图像保存在内部存储器中,我遇到了一些麻烦.我尝试了几种方法来执行此操作,并且大多数方法似乎都可以使用.但是当我尝试使用它们时,图片没有被保存,或者至少我没有找到要保存图片的文件夹.

This process works fine, the user gets the picture and this is shown in an imageview before it's saved. But to save the image in the internal storage, I'm having some trouble. I have tried several ways to do it, and most of them seem to work. But when I try them, the picture is not being saved or at least I don't find the folder where it is being saved.

我尝试了以下3种方式:

I have tried in these 3 ways:

第一:

File directory = getDir("profile", Context.MODE_PRIVATE);
File mypath = new File(directory, "thumbnail.png");

FileOutputStream fos = null;
try {
    fos = new FileOutputStream(mypath);
    mybitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
    fos.close();
} catch (Exception e) {
    e.printStackTrace();
}

第二:

ByteArrayOutputStream bytes = new ByteArrayOutputStream();
mybitmap.compress(Bitmap.CompressFormat.PNG, 90, bytes);

FileOutputStream fos = null;
try {
    fos = openFileOutput("thumbnail.png", Context.MODE_PRIVATE);
    fos.write(bytes.toByteArray());
    fos.close();
} catch (FileNotFoundException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}

第三:

ByteArrayOutputStream bytes = new ByteArrayOutputStream();
mybitmap.compress(Bitmap.CompressFormat.PNG, 90, bytes);

File fileWithinMyDir = new File(getFilesDir(), "thumbnail.png");
try {
    FileOutputStream fos = new FileOutputStream(fileWithinMyDir);
    fos.write(bytes.toByteArray());
    fos.close();
} catch (FileNotFoundException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}

应该将图像保存在以下路径中:android/data/AppName/app_data/,但未在其中创建文件夹.无论如何,我已经在其他文件夹中查找了,但是什么也没有.

Suposedly, the image gets saved in the path: android/data/AppName/app_data/ but there is no folder created there. Anyway I have looked in other folders, but nothing.

编辑-

在第一种方法中,我已经看到抛出异常:

In the first method, I've seen that is throwing an exception:

E/SAVE_IMAGE﹕ /data/data/com.example.myapp/app_profile/thumbnail.png: open failed: EISDIR (Is a directory)
java.io.FileNotFoundException: /data/data/com.example.myapp/app_profile/thumbnail.png: open failed: EISDIR (Is a directory)
Caused by: libcore.io.ErrnoException: open failed: EISDIR (Is a directory)

推荐答案

尝试了几件事之后,这终于对我有用:

After trying several things, this is what finally has worked for me:

ContextWrapper cw = new ContextWrapper(getApplicationContext());
File directory = cw.getDir("profile", Context.MODE_PRIVATE);
if (!directory.exists()) {
    directory.mkdir();
}
File mypath = new File(directory, "thumbnail.png");

FileOutputStream fos = null;
try {
    fos = new FileOutputStream(mypath);
    resizedbitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
    fos.close();
} catch (Exception e) {
    Log.e("SAVE_IMAGE", e.getMessage(), e);
}

基本上,这是检查目录(而不是文件)是否存在,如果不存在,请使用mkdir()创建它.

Basically the thing is to check if the directory (not the file) exists, and if not, create it with mkdir().

这篇关于将位图保存到应用程序文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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