如何更改位图保存位置

时间:2018-05-22 14:28:57

标签: android bitmap directory

如何更改图像保存位置我已创建文件夹,但如何在其中保存图像。所有下载的图像都保存在图片文件夹中

   @Override
        public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
            ContentResolver r = contentResolverWeakReference.get();
            AlertDialog alertDialog = alertDialogWeakReference.get();
            if (r != null)
                file = new File(Environment.getExternalStorageDirectory().getPath() + "/CreativeGraphy");
            if (!file.exists()) {
                file.mkdir();
            }
            try {
                file.createNewFile();
                MediaStore.Images.Media.insertImage(r, bitmap, name, desc);

            } catch (Exception e) {
                            e.printStackTrace();

            }
            alertDialog.dismiss();
            Toast.makeText(context, "Download succeed ", Toast.LENGTH_SHORT).show();
        }

2 个答案:

答案 0 :(得分:0)

使用此方法

public static void saveBitmap(String path, Bitmap bitmap) {
    FileOutputStream out = null;
    try {
        out = new FileOutputStream(path);
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            if (out != null) {
                out.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

保存后,您可以调用 scanFile 方法索引图库中的文件。

MediaScannerConnection.scanFile(context, new String[]{path}, null, null);

答案 1 :(得分:0)

谢谢大家这是有效的

 public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
    ContentResolver r = contentResolverWeakReference.get();
    AlertDialog alertDialog = alertDialogWeakReference.get();
    if (r != null)
       file_path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/CreativeGraphy";
    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();
}