java - 如何将位图保存到android中的应用程序文件夹

标签 java android android-file

我有一个位图,我想将其保存到应用程序文件夹中。我尝试使用这些代码:

 ContextWrapper contextWrapper = new ContextWrapper(context.getApplicationContext());
 File directory = contextWrapper.getDir("tabs", Context.MODE_PRIVATE);
 if (!directory.exists())
     directory.mkdir();
     String fname = "Image.jpg";
     File file = new File(directory, fname);
     FileOutputStream fos = null;
     try {
         fos = new FileOutputStream(file);
         bitmap.compress(Bitmap.CompressFormat.JPEG, 50, fos);
         fos.close();
     } catch (Exception e) {
            Log.e("SAVE_IMAGE", e.getMessage(), e);
     }

现在,我有两个问题。

  1. 为什么会显示此警告以及如何修复它?

Result of 'File.mkdir()' is ignored

  • 在应用程序文件夹中创建了目录“app_tabs”,但未保存位图,并且该文件夹中没有照片。如何保存该位图?
  • ScreenShot

    最佳答案

    你可以这样做:

    try {
         FileOutputStream fileOutputStream = context.openFileOutput("Your File Name", Context.MODE_PRIVATE);
         bitmap.compress(Bitmap.CompressFormat.PNG, 100, fileOutputStream);
         fileOutputStream.close();
     } catch (Exception e) {
         e.printStackTrace();
     }
    

    它将您的位图保存在应用程序文件夹的“files”目录中。

    关于java - 如何将位图保存到android中的应用程序文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61382955/

    相关文章:

    java - 家庭作业帮助,Java Pair 类(class)

    java - Java “try without catch”和 “catch without try”

    安卓网络服务

    java - 如何在 If Else 语句 Android 中声明和初始化变量?

    android.os.FileUriExposedException : file:///storage/emulated/0/test. txt 通过 Intent.getData() 暴露在应用之外

    java - 当在 FileWriter 中追加 "true"时,项目从列表中删除

    Android,如何选择保存文件位置?

    java - 如何在 Java Netbeans 平台模块化项目中设置 VM 选项?

    java - 关于克服 Java 中缺乏多重继承的任何既定做法?

    android - Android 到 Java 的反汇编程序