Store/Save ImageView image in Gallery in android programmatically

How to save image inside mobile phone’s gallery from android app’s drawable resource folder.

In this tutorial we are saving imageview image which is already stored inside android projects drawable folder. We are fetching imageview image and save that image inside mobile phones gallery. So here is the complete step by step tutorial for Store/Save ImageView image in Gallery in android programmatically.

android-project-download-code-button

Note: Please download sample image from below and copy inside drawable-hdpi folder.

drawable hdpi folder

Below is the sample image. Download this image and put inside drawable-hdpi folder.

demo_image

How to Store/Save ImageView image in Gallery in android programmatically.

Note : Please add WRITE_EXTERNAL_STORAGE permission inside your project AndroidManifest.xml file.

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

Code for MainActivity.java file.

 package com.saveimageviewimagegallery_android_examples.com;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends Activity {

 ImageView imageview;
 Button button;
 Drawable drawable;
 Bitmap bitmap;
 String ImagePath;
 Uri URI;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 button = (Button)findViewById(R.id.button1);
 imageview = (ImageView)findViewById(R.id.imageView1);
 
 
 button.setOnClickListener(new View.OnClickListener() {
 
 @Override
 public void onClick(View v) {
 // TODO Auto-generated method stub
 
 drawable = getResources().getDrawable(R.drawable.demo_image);

 bitmap = ((BitmapDrawable)drawable).getBitmap();

 ImagePath = MediaStore.Images.Media.insertImage(
 getContentResolver(),
 bitmap,
 "demo_image",
 "demo_image"
 );

 URI = Uri.parse(ImagePath);

 Toast.makeText(MainActivity.this, "Image Saved Successfully", Toast.LENGTH_LONG).show();
 
 }
 });
 
 }
}

Code for activity_main.xml layout file.

 <RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
 android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin"
 tools:context="com.saveimageviewimagegallery_android_examples.com.MainActivity" >

 <ImageView
 android:id="@+id/imageView1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignParentTop="true"
 android:layout_centerHorizontal="true"
 android:src="@drawable/demo_image" />

 <Button
 android:id="@+id/button1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_below="@+id/imageView1"
 android:layout_centerHorizontal="true"
 android:layout_marginTop="21dp"
 android:text="Click here to Store/Save ImageView image in Gallery in android programmatically" />

</RelativeLayout>

Code for AndroidManifest.xml file.

 <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
 package="com.saveimageviewimagegallery_android_examples.com"
 android:versionCode="1"
 android:versionName="1.0" >

 <uses-sdk
 android:minSdkVersion="8"
 android:targetSdkVersion="21" />

 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
 
 <application
 android:allowBackup="true"
 android:icon="@drawable/ic_launcher"
 android:label="@string/app_name"
 android:theme="@style/AppTheme" >
 <activity
 android:name=".MainActivity"
 android:label="@string/app_name" >
 <intent-filter>
 <action android:name="android.intent.action.MAIN" />

 <category android:name="android.intent.category.LAUNCHER" />
 </intent-filter>
 </activity>
 </application>

</manifest>

Screenshot:

Store/Save ImageView image in Gallery in android programmatically

Click here to download Store/Save ImageView image in Gallery in android programmatically project with source code.

4 Comments

  1. Kuforiji Oluwafemi

    how do you do the reverse, my setImageBitmap seems not to work

    • Sir this project is working file if have facing any type of error then please download and import the whole project into Studio or Eclipse then run it.

  2. Is it possible to save GIF image??

Leave a Reply

Your email address will not be published. Required fields are marked *