f2prateek
f2prateek

Reputation: 2084

Saving a bitmap to disk/gallery in Android 4.3

I've been using the way the system saves screenshots to save my bitmaps to the disk and gallery. This works in Android 4.2 and before but not in Android 4.3.

Relevant code :

Uri imageUri = resolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
OutputStream out = resolver.openOutputStream(uri);

Full code here.

In 4.3 (new Nexus 7) however, I get a FileNotFoundException on the second line. I couldn't see any changes in 4.3 relevant to this on the website.

So what is the right way to save an image to the disk and gallery?

Verified :

Upvotes: 7

Views: 2811

Answers (4)

Goo
Goo

Reputation: 1328

There some changes in the fileSystem on Android 4.3 to start to avoid dev. to directly write in "/sdcard" or "/mnt/sdcard" but use the android ExternalStorage system. (http://source.android.com/devices/tech/storage/index.html)

N.B. : ExternalStorage can be an internal memory :p

For your case, have you tryed to use a method based on getExternalStorage ? (like this : Find an external SD card location)

Upvotes: 0

Azhar Yousuf
Azhar Yousuf

Reputation: 111

I have these permission in my Manifest.

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

But I am using a Target SDK version 15. Is there a requirement that you have to use a target SDK 18?

BTW:

Here is a sample code for downloading profile pictures from Facebook:

private class DownloadProfilePicTask extends AsyncTask<Void,String,String> {
    Bitmap profilePic;
    String fileName;
    String id;
    String type;
    URL img_value;
    public DownloadProfilePicTask(String i,String ty)
    {
        id = i;
        if(id==null)
        {
            //Log.v("Id is null", "Error");
        }
        //Log.v("Download Profile Pic Task initialized for id:",id);
        type = ty;
    }
    @Override
    protected String doInBackground(Void...param) {
         String root = Environment.getExternalStorageDirectory().toString();
         if(root==null)
         {
             return null;
         }
         try{
         profilePic = BitmapFactory.decodeStream(img_value.openConnection().getInputStream());
         }
         catch (IOException e) {
             e.printStackTrace();

         }

        if(profilePic == null)
        {
            //Log.v("profilePic is null", "Error");
        }
         //Log.v("Root for saving images",root );
         File myDir = new File(root + "/saved_images");
         myDir.mkdirs();
        fileName = root + "/saved_images/" + id + ".png";
        //Log.v("filename is ",fileName);
         File file = new File (fileName); 
         fileName = file.getPath();
         if (file.exists ()) file.delete (); 
         try {
                FileOutputStream out = new FileOutputStream(file);
                profilePic.compress(Bitmap.CompressFormat.PNG, 90, out);
                out.flush();
                out.close();
         } catch (Exception e) {
                e.printStackTrace();
         }
         return id;
    }
    @Override
    protected void onPreExecute()
    {
        try
        {
            img_value = new URL("http://graph.facebook.com/"+id+"/picture?type=" + type);
        }
         catch (MalformedURLException e) {
                e.printStackTrace();

        } 

    }
    @Override
    protected void onPostExecute(String result) {
    }
}

and then I just call:

new DownloadProfilePicTask(id,type).execute();

to download and automatically save images.

Note: You will have to play with filePath a bit for exact location.

Upvotes: 0

Phil
Phil

Reputation: 36299

This is the way I save bitmaps to the Gallery:

Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
Uri contentUri; //instantiate Uri with location of image
mediaScanIntent.setData(contentUri);
context.sendBroadcast(mediaScanIntent);

Upvotes: 1

T_V
T_V

Reputation: 17580

In your manifest file try with change target sdk to 18.-

  <uses-sdk android:minSdkVersion="7" 
    android:targetSdkVersion="18"/>

It might solve your prob(May not). In 4.3 JELLY_BEAN_MR2, android did couple of changes and android clearly written that Your app might misbehave in a restricted profile environment. so please look at http://developer.android.com/about/versions/android-4.3.html

Upvotes: 0

Related Questions