unknown os platform
how to save an image to gallery in android programmatically | Whats-online.info
whats-online-info

how to save an image to gallery in android programmatically


how
2017/03/17 / Science and Tutorials

Android tutorial: Learn how to save an image to gallery from an imageview, drawable folder or camera capture programmatically. See an example





save image to gallery android programmatically


Today we are going to learn how to save an image to gallery from an imageview, drawable folder or camera capture url programmatically.

You can download the source code demo example at the end of this tutorial.

First and foremost, we need to set up our project to demonstrate this post. For the newbie, follow the link below.

We will use our default MainActivity.java and activity_main.xml to demonstrate the post. 

 MainActivity.java Entire Code


package info.whats_online.savetogallery; import android.app.Activity; import android.graphics.Bitmap; import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable; import android.media.MediaScannerConnection; import android.net.Uri; import android.os.Bundle; import android.os.Environment; import android.util.Log; import android.view.View; import android.widget.Toast; import java.io.File; import java.io.FileOutputStream; import java.util.Random; public class MainActivity extends Activity {     Bitmap anImage;     @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);         Drawable myDrawable = getResources().getDrawable(R.drawable.dor);         anImage      = ((BitmapDrawable) myDrawable).getBitmap();     }     public void BtnClick(View v){         int e=v.getId();         switch(e){             case R.id.save:                 saveImageToExternalStorage(anImage);                 Toast.makeText(getApplicationContext(), "Saved successfully, Check gallery", Toast.LENGTH_SHORT).show();                 break;         }     }     private void saveImageToExternalStorage(Bitmap finalBitmap) {         String root = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString();         File myDir = new File(root + "/saved_images_1");         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();         }         // Tell the media scanner about the new file so that it is         // immediately available to the user.         MediaScannerConnection.scanFile(this, new String[]{file.toString()}, null,                 new MediaScannerConnection.OnScanCompletedListener() {                     public void onScanCompleted(String path, Uri uri) {                         Log.i("ExternalStorage", "Scanned " + path + ":");                         Log.i("ExternalStorage", "-> uri=" + uri);                     }                 });     } }


activity_main.xml Entire Code 


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:orientation="vertical"     tools:context=".MainActivity">     <Button         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:id="@+id/save"         android:text="Save"         android:onClick="BtnClick"         /> </LinearLayout>

How does this post demo work?
 We need to get our image from imageview, camera capture or from drawable and convert it to bitmap. We will pass our bitmap image to the function to save to the gallery as shown below

Not forgetting, we need to add MediaScannerConnection class inside our function to scan for new files or folders added to our gallery. Note, if the function below is omitted, our files saved to the external storage are only visible after restarting the entire android device. So the function will keep on scanning for new files and folders added from our app to the gallery.

 MediaScannerConnection.scanFile(this, new String[]{file.toString()}, null,                 new MediaScannerConnection.OnScanCompletedListener() {                     public void onScanCompleted(String path, Uri uri) {                         Log.i("ExternalStorage", "Scanned " + path + ":");                         Log.i("ExternalStorage", "-> uri=" + uri);                     }                 });

Thanks for your time. Subscribe to our newsletter to get the latest updates direct to your mailbox.












You may also like:


Leave a comment












Subscribe to get replies direct to your mailbox

Submit comment