【问题标题】:Need to copy image from res folder to gallery/folder需要将图像从 res 文件夹复制到画廊/文件夹
【发布时间】:2012-09-16 13:59:57
【问题描述】:

好的,我有画廊应用程序,里面有很多图像(res/drawable)。

选择后,您可以设置为墙纸按钮,您将拥有它。

现在我想使用按钮保存到手机或 SD 卡保存此图像。我该如何管理。从应用程序文件夹的 res 复制到手机或 SD 卡。不想从 ImageView 中获取它,而只是将原件从 res 复制到 Phone。

【问题讨论】:

标签: android image copy gallery drawable


【解决方案1】:

试试这个代码:

  String root = Environment.getExternalStorageDirectory().toString();
  File myDir = new File(root + "/saved_images");    
  myDir.mkdirs();
  Random generator = new Random();
  int n = 10000;
  n = generator.nextInt(n);
  String fname = "Image-"+ n +".jpg";
  File file = new File (myDir, fname);
  if (file.exists ()) file.delete (); 
  try {
       FileOutputStream out = new FileOutputStream(file);
       finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
       out.flush();
       out.close();

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

并在清单文件中添加:

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

【讨论】:

    【解决方案2】:

    按照步骤操作:-

    代码:

    String path = Environment.getExternalStorageDirectory().toString();
    OutputStream fOut = null;
    file = new File(path, "image.jpg");
    fOut = new FileOutputStream(file);
    Bitmap bitmap = BitmapFactory.decodeResource (getResources(), R.drawable.xyz);
    bitmap.compress(Bitmap.CompressFormat.JPEG, 85, fOut);
    fOut.flush();
    fOut.close();
    MediaStore.Images.Media.insertImage(getContentResolver(),file.getAbsolutePath(),file.getName(),file.getName());
    

    【讨论】:

    • 这个工作正常。现在我如何设置文件名和要保存的文件夹,因为现在我正在获取图像到 /DCIM/Camera/somerandomnumber.jpg
    • 您将随机数作为名称,因为可能已经存在带有image.jpg 名称的图像。因此,请使用一些唯一的名称(如当前日期时间)。检查方法MediaStore.Images.Media.insertImagehere,第三个参数是图片的名称。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多