【问题标题】:Save a bitmap in internal cache folder在内部缓存文件夹中保存位图
【发布时间】:2017-01-30 17:06:11
【问题描述】:

我需要做几件简单的事情...但我做不到

  • 将位图保存到内部缓存以备后用,现在我保存名称(例如:name_2347458.jpg)

我试过这种方法:

public static void saveFile(Context context, Bitmap bitmap, String picName) {
    FileOutputStream fileOutputStream;
    try {
        fileOutputStream = context.openFileOutput(picName, Context.MODE_PRIVATE);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 30, fileOutputStream);
        fileOutputStream.close();
    } catch (FileNotFoundException e) {
        Log.d(TAG, "file not found");
        e.printStackTrace();
    } catch (IOException e) {
        Log.d(TAG, "io exception");
        e.printStackTrace();
    }

}

它保存文件但我不知道在哪里?

使用 getExternalCacheDir() 我可以看到设备上的文件,但不能使用 openFileOutput。

我可以加载位图:

    public static Bitmap loadBitmap(Context context, String picName) {

    Bitmap bitmap = null;
    FileInputStream fileInputStream;
    try {
        fileInputStream = context.openFileInput(picName);
        bitmap = BitmapFactory.decodeStream(fileInputStream);
        fileInputStream.close();

    } catch (FileNotFoundException e) {
        Log.d(TAG, "file not found");
        e.printStackTrace();
    } catch (IOException e) {
        Log.d(TAG, "io exception");
        e.printStackTrace();
    }
    return bitmap;
}

但还是只使用名称

【问题讨论】:

    标签: java android file bitmap storage


    【解决方案1】:

    它保存文件但我不知道在哪里?

    它保存在internal storage

    我可以用

    加载位图

    那么您的代码似乎可以正常工作。

    【讨论】:

    • 它应该在 Android/data/packageName/cache 之类的地方?但我没有看到任何带有我的应用程序包名称的目录
    • @An-droid:这在the blog post that I linked to in my answer 中都有介绍。无法使用普通工具浏览内部存储。
    猜你喜欢
    • 2021-08-12
    • 1970-01-01
    • 2011-09-20
    • 2013-03-17
    • 1970-01-01
    • 1970-01-01
    • 2021-08-17
    • 1970-01-01
    • 2013-12-26
    相关资源
    最近更新 更多