Good afternoon friends There was a problem in the following, in fact there is an application on the android in it in the / res / drawable folder the images are stored, these images are displayed on the linearlayout as background'a so. It is necessary that by pressing the button the picture that is now on the screen as the background'a is saved, let's say, in the / mnt / sdcard / myimgfolder folder. If anyone has any thoughts on this, I will be grateful

Zy foreign sites offer some kind of solutions, but they are not suitable, they offer to choose a picture from a standard gallery and save it to a sdcard (why is it not clear :)). I'm new to the development of an android so do not kick hard. ATP in advance

    2 answers 2

    First, you need additional permission:

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

    Secondly, to find out the root of this folder, use the function:

     File extStorage = Environment.getExternalStorageDirectory(); 

      Draw drawable with LinearLayout.

        myDrawable = myLinear.getBackground(); 

      Convert to bitmap

       Bitmap myImage= ((BitmapDrawable)myDrawable).getBitmap(); 

      // this is from the current linearLayout (or whatever you have), if with res / drawable then

       Bitmap myImage= BitmapFactory.decodeResource(context.getResources(), R.drawable.imageres); 

      and save it

       String path = Environment.getExternalStorageDirectory().toString(); OutputStream fOut = null; File file = new File(path, "image.jpg"); fOut = new FileOutputStream(file); myImage.compress(Bitmap.CompressFormat.JPEG, 85, fOut); fOut.flush(); fOut.close(); 

      All found on a foreign site. Add to the manifest what Dex indicated earlier.

      • Doing saving directly to the forehead is a bad idea. First you use File extStorage = Environment.getExternalStorageDirectory (); to find out if the directory is available for writing, and then start saving. - Dex
      • Well, I think to check for the availability of a sdcard, and then write it down, although checking for a record would also do it, ATP for advice - alogin
      • @dex, make a check on the SD card is a minute of affairs .. I do not think that a person will have problems with this - PeterDroid
      • Something it does not work, I press a button and nothing happens, could you give a link where did you get the code or show all the code - alogin
      • @alogin stackoverflow.com/questions/7021728/… for example .. in Google, set save bitmap to sdcard android java and you will find even more examples - PeterDroid