简体   繁体   English

如何将 bitmap 保存到 android 中的应用程序文件夹

[英]How to Save bitmap to app folder in android

I have a bitmap that I want to save it to the app folder.我有一个 bitmap,我想将它保存到 app 文件夹中。 I try with these codes:我尝试使用这些代码:

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

Now, I have two problems.现在,我有两个问题。

  1. Why this warning show and how to fix it?为什么显示此警告以及如何解决?

Result of 'File.mkdir()' is ignored 'File.mkdir()' 的结果被忽略

  1. Directory "app_tabs" was created in the application folder, But not saved bitmap and There is no photo in the folder.在应用程序文件夹中创建了目录“app_tabs”,但未保存 bitmap 并且文件夹中没有照片。 How do I save this bitmap?如何保存这个 bitmap?

截屏

You can just do this:你可以这样做:

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

It saves your bitmap in the "files" directory in app folder.它将您的 bitmap 保存在应用文件夹的“文件”目录中。

Technically your codes is correct, also I had tried on my own device all is correct.从技术上讲,您的代码是正确的,我也曾在自己的设备上尝试过,一切都是正确的。

So I suspect you find the other path, or you need to click 'Synchronize' menu if you use Android Studio to check file.所以我怀疑您找到了其他路径,或者如果您使用 Android Studio 检查文件,则需要单击“同步”菜单。

By the way, you can just use context or context.getApplicationContext() to call getDir(), no need to new a ContextWrapper.顺便说一句,您可以只使用 context 或 context.getApplicationContext() 来调用 getDir(),无需新建 ContextWrapper。 like this:像这样:

File directory = context.getDir("tabs", Context.MODE_PRIVATE);
 String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).toString() + "/tabs";
        File dir = new File(path);
        if (!dir.exists())
            dir.mkdirs();

        File futureStudioIconFile = new File(path, "Image.jpg);
        if (futureStudioIconFile.exists())
            futureStudioIconFile.delete();
        futureStudioIconFile.createNewFile();

try this,, i wish it helps试试这个,我希望它有帮助

this is my answer这是我的答案

  1. Result of 'File.mkdir()' is ignored : Because mkdir() return a boolean, true if the folder is created and false otherwwise. Result of 'File.mkdir()' is ignored :因为 mkdir() 返回 boolean,如果创建文件夹则为true ,否则为 false。

  2. This is how I save bitmap这就是我保存 bitmap 的方法

        File storageDir = getFilesDir();

        File filePath = File.createTempFile(
                imageFileName,  /* prefix */
                ".jpg",         /* suffix */
                storageDir      /* directory */
        );


        try (FileOutputStream out = new FileOutputStream(filePath)) {
            capturedImg.compress(Bitmap.CompressFormat.JPEG, quality, out);
        } catch (IOException e) {
            e.printStackTrace();
        }

or if you just want to save the bitmap at original resolution或者如果您只想以原始分辨率保存 bitmap

    public static void copy(Uri uri, File dst, Context context) {
        try (InputStream in = context.getContentResolver().openInputStream(uri);
             OutputStream out = new FileOutputStream(dst)) {
            // Transfer bytes from in to out
            byte[] buf = new byte[1024];
            int len;
            while ((len = in.read(buf)) > 0) {
                out.write(buf, 0, len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM