好的,所以我有Gallery Application,里面有很多图片(res / drawable)。
在选择时,您可以设置为壁纸按钮,您将拥有它。
现在我想用按钮SAVE TO PHONE或SD卡保存,选择此图像。我该怎么办呢。从应用程序文件夹的res复制到手机或SD卡。不想从ImageView中获取它,只需将原始文件从res复制到手机。
答案 0 :(得分:2)
试试这段代码:
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" />
答案 1 :(得分:1)
按照以下步骤操作: -
Bitmap
OutputStream
代码:
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());