【问题标题】:How to save Image to external Storage in android? [duplicate]如何将图像保存到android中的外部存储? [复制]
【发布时间】:2019-01-31 16:29:44
【问题描述】:

我想将位图图像存储在外部存储中,但创建文件目录时出错。

这是我的代码。

private void saveImage(Bitmap bitmap){
        String root = Environment.getExternalStorageDirectory().toString();
        File directory = new File(root + "/Wallpapers");
        boolean wasSuccessful = directory.mkdirs();
        if(!wasSuccessful){
            Toast.makeText(context, "Error Creating directory", Toast.LENGTH_SHORT).show();
        }
        Random generator = new Random();
        int n = 10000;
        n = generator.nextInt();
        String fname = "Wallpaper-"+n+".png";
        File file = new File(directory, fname);
        if (file.exists()){
            file.delete();
        }
        try {
            FileOutputStream out = new FileOutputStream(file);
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
            out.flush();
            out.close();
            Toast.makeText(context, "Wallpaper Saved Successfully", Toast.LENGTH_SHORT).show();
        }catch (Exception e){
            Toast.makeText(context, "Error Saving Wallpaper", Toast.LENGTH_SHORT).show();
            e.printStackTrace();
        }

    }

我已经在android manifest中写了权限。

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

我已经尝试了很多解决方案,但都无法解决我的问题。

【问题讨论】:

  • 权限检查???
  • 你在运行时检查权限了吗?
  • 在manifest文件中写权限是不够的!您必须始终要求用户提供并在每次使用前检查。
  • @MUmer 保存图像时遇到什么错误?
  • 请看this

标签: java android android-permissions


【解决方案1】:

编写简单的例子来存储 bitmapImage 给出的图像如下。请尝试一下...

    //Get file name and bitmapImage and call the method ()
    //Bitmap bitmapImage  = ImageUtil.getInstance().changeImageRotated(cameraUri, bitmapImage, ImageUtil.TypeMode.CAMERA);
    //String fileName = String.valueOf(getCurrentTimeStamp());
    //ImageUtil.getInstance().saveImage(bitmapImage, fileName);

    //Here is function to save bitmap image to internal storage
    public Boolean saveImage(Bitmap imageData, String fileName) {
    String savePath = getFilePath(AppConst.getInstance().IMAGE_DIR_APPLY);
    isPathMade(savePath);

    File file = new File(savePath, fileName);

    FileOutputStream fileOutputStream = null;
    ObjectOutputStream objectOutputStream = null;
    boolean keep = true;

    try {
        fileOutputStream = new FileOutputStream(file);
        imageData.compress(Bitmap.CompressFormat.PNG, AppConst.getInstance().PARKING_APPLY_IMAGE_COMPRESS_QUALITY, fileOutputStream);
    } catch (Exception e) {
        keep = false;
    } finally {
        try {
            if (objectOutputStream != null) {
                objectOutputStream.close();
            }
            if (fileOutputStream != null){
                fileOutputStream.close();
            }
            if (!keep) {
                file.delete();
            }
            return true;
        } catch (Exception e) {
            Logger.e(TAG, "saveImageToCache Exception is " + e.toString());
        }
    }
    return false;
}

【讨论】:

    猜你喜欢
    • 2013-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-28
    • 1970-01-01
    • 2015-09-24
    • 2018-04-20
    • 2015-06-14
    相关资源
    最近更新 更多