如何使用位图图像保存大图像超过1-2MB

时间:2018-08-29 03:12:30

标签: android

我的代码适用于较小的文件。当我尝试使用具有较大文件大小的高分辨率图像时,它无法保存问题

Helper类有助于将图像保存在设备中,但有时不会将图像保存在给定的文件夹中,图像会转到Pictures文件夹,而其他情况是当我使用3 Mb图像时无法保存。

 public class SaveImageHelper implements Target {

    private Context context;
    private WeakReference<AlertDialog> alertDialogWeakReference;
    private WeakReference<ContentResolver> contentResolverWeakReference;
    private String name, desc, file_path;

    public SaveImageHelper(Context context, AlertDialog alertDialog, ContentResolver contentResolver, String name, String desc) {
        this.context = context;
        this.alertDialogWeakReference = new WeakReference<AlertDialog>(alertDialog);
        this.contentResolverWeakReference = new WeakReference<ContentResolver>(contentResolver);
        this.name = name;
        this.desc = desc;
    }

    @Override
    public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
        ContentResolver r = contentResolverWeakReference.get();
        AlertDialog alertDialog = alertDialogWeakReference.get();
        if (r != null)
            file_path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/CG";
        File dir = new File(file_path);
        if (!dir.exists()) {
            dir.mkdir();
        }
        File file = new File(dir, name);
        FileOutputStream fOut;
        try {
            MediaStore.Images.Media.insertImage(r, bitmap, name, desc);
            fOut = new FileOutputStream(file);
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut);
            fOut.flush();
            fOut.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
        alertDialog.dismiss();
        Toast.makeText(context, "Download succeed ", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onBitmapFailed(Drawable errorDrawable) {

    }

    @Override
    public void onPrepareLoad(Drawable placeHolderDrawable) {

    }
}

0 个答案:

没有答案