Tuesday, September 29, 2015

android - How to save an image to gallery

activity_main.xml

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/rl"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="10dp"
    tools:context=".MainActivity"
    android:background="#e0f5d4"
    >
    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Save Image To Gallery"
        />
    <ImageView
        android:id="@+id/iv_source"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/btn"
        android:padding="5dp"
        android:src="@drawable/bird"
        />
    <TextView
        android:id="@+id/tv_source"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/iv_source"
        android:text="Original Image"
        />
    <ImageView
        android:id="@+id/iv_saved"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/btn"
        android:layout_toRightOf="@id/iv_source"
        android:padding="5dp"
        />
    <TextView
        android:id="@+id/tv_saved"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/iv_saved"
        android:layout_alignLeft="@id/iv_saved"
        />
</RelativeLayout>
MainActivity.java

package com.cfsuman.me.androidcodesnippets;

import android.app.ActionBar;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.BitmapDrawable;
import android.widget.TextView;
import android.provider.MediaStore;


public class MainActivity extends Activity {

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Get the widgets reference from XML layout
        final RelativeLayout rl = (RelativeLayout) findViewById(R.id.rl);
        final Button btn = (Button) findViewById(R.id.btn);
        final ImageView iv_source = (ImageView) findViewById(R.id.iv_source);
        final ImageView iv_saved = (ImageView) findViewById(R.id.iv_saved);
        final TextView tv_saved = (TextView) findViewById(R.id.tv_saved);

        final ActionBar ab = getActionBar();
        ab.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#FFB4C7AA")));

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // Get the image from drawable resource as drawable object
                Drawable drawable = getDrawable(R.drawable.bird);

                // Get the bitmap from drawable object
                Bitmap bitmap = ((BitmapDrawable)drawable).getBitmap();

                /*
                    public static final String insertImage (ContentResolver cr, Bitmap source,
                    String title, String description)

                        Insert an image and create a thumbnail for it.

                    Parameters
                        cr : The content resolver to use
                        source : The stream to use for the image
                        title : The name of the image
                        description : The description of the image

                    Returns
                        The URL to the newly created image, or null if the image
                        failed to be stored for any reason.
                */

                // Save image to gallery
                String savedImageURL = MediaStore.Images.Media.insertImage(
                        getContentResolver(),
                        bitmap,
                        "Bird",
                        "Image of bird"
                );

                // Parse the gallery image url to uri
                Uri savedImageURI = Uri.parse(savedImageURL);

                // Display the saved image to ImageView
                iv_saved.setImageURI(savedImageURI);

                // Display saved image url to TextView
                tv_saved.setText("Image saved to gallery.\n" + savedImageURL);
           }
        });
    }
}
AndroidManifest.xml (permission)

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

More android examples