2

Ok, so i have Gallery Application, with lots of images in it(res/drawable).

On selection you can Set as Wallpaper button and you will have it.

Now i want to save with button SAVE TO PHONE or SD card this image selected. How can i manage that. Copying from res of application folder to Phone or SD card. Dont want to take it from ImageView but just copy the original from res to Phone.

Allen
  • 65
  • 9
  • You can find an answer which should do for you here http://stackoverflow.com/questions/939170/resources-openrawresource-issue-android – dorjeduck Sep 25 '12 at 05:06

2 Answers2

2

try this code:

  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();
}

and add in the manifest file:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
SubbaReddy PolamReddy
  • 2,083
  • 2
  • 17
  • 23
1

Follow the steps :-

Code:

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());
vKashyap
  • 580
  • 6
  • 17
  • this one is working. Now how can i set file name and what folder to save, cuz now i am getting image to /DCIM/Camera/somerandomnumber.jpg – Allen Sep 25 '12 at 17:48
  • You are getting random number as name because there might be an image already present with `image.jpg` name. So, use some unique name(like the present date time). check the mehtod `MediaStore.Images.Media.insertImage` [here](http://developer.android.com/reference/android/provider/MediaStore.Images.Media.html), the third parameter is the name of the image. – vKashyap Sep 26 '12 at 06:02