Copying an image to another in ANDROID.

By | April 7, 2012

Here is a simple example to copy an image to another image file in ANDROID.
In this example a button click will open the gallery and after that it will be copied to another image file.
You have to check the File-explorer to find the file.

This is the java code for doing it.

package pack.coderzheaven;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.channels.FileChannel;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class CopyingImageFromGalleryDemo extends Activity {

	String mSelectedImagePath;
	private static final int SELECT_IMAGE = 1;
	Button b;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        b = (Button)findViewById(R.id.Button01);
        b.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				imageFromGallery();
			}
		});
    }

    /*** this function opens the gallery and gets the image ***/
    public void imageFromGallery() {
    	Intent intent = new Intent();
        intent.setType("image/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        startActivityForResult(Intent.createChooser(intent,"Select Picture"), SELECT_IMAGE);
    }

    /* After opening gallery control comes here */
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
    	  super.onActivityResult(requestCode, resultCode, data);
    	  if (resultCode == RESULT_OK) {
    	    switch(requestCode) {
	    	    case SELECT_IMAGE:
	    	        mSelectedImagePath = getPath(data.getData());
	    	        System.out.println("mSelectedImagePath : " + mSelectedImagePath);
	    	        try {
	    	    		File sd = Environment.getExternalStorageDirectory();
	    	    		if (sd.canWrite()) {
	    	    			System.out.println("(sd.canWrite()) = " + (sd.canWrite()));
	    	    		    String destinationImagePath= "/file.jpg";	// this is the destination image path.
	    	    		    File source = new File(mSelectedImagePath );
	    	    		    File destination= new File(sd, destinationImagePath);
	    	    		    if (source.exists()) {
	    	    		        FileChannel src = new FileInputStream(source).getChannel();
	    	    		        FileChannel dst = new FileOutputStream(destination).getChannel();
	    	    		        dst.transferFrom(src, 0, src.size());		// copy the first file to second.....
	    	    		        src.close();
	    	    		        dst.close();
	    	    		        Toast.makeText(getApplicationContext(), "Check the copy of the image in the same path as the gallery image. File is name file.jpg", Toast.LENGTH_LONG).show();
	    	    		    }
	    	    		}else{
	    	    			Toast.makeText(getApplicationContext(), "SDCARD Not writable.", Toast.LENGTH_LONG).show();
	    	    		}
	    	    	}catch (Exception e) {
	    	    		System.out.println("Error :" + e.getMessage());
	    	    	}
	    	        break;
    	  }
    	}
    }

	public String getPath(Uri uri) {
		String[] projection = { MediaStore.Images.Media.DATA };
		Cursor cursor = managedQuery(uri, projection, null, null, null);
		startManagingCursor(cursor);
		int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
		cursor.moveToFirst();
		return cursor.getString(column_index);
	}
}

here is the main.xml file

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello"
    />
<Button
	android:text="Gallery"
	android:id="@+id/Button01"
	android:layout_width="wrap_content"
	android:layout_height="wrap_content">
</Button>
</LinearLayout>

Then the strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello"><b>Copying an Image to another Demo From CoderzHeaven</b></string>
    <string name="app_name">Copy an Image to another</string>
</resources>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="pack.coderzheaven"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".CopyingImageFromGalleryDemo"
                  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>
Copy Image Demo

Copy Image Demo

Copy Image Demo

Copy Image Demo

Change the name of the file as you like.
Enjoy…………..

Please leave your comments and click on the +1 button above.

4 thoughts on “Copying an image to another in ANDROID.

  1. Pingback: Android » Archive » Copying an image to another in ANDROID. | Coderz Heaven » Android

  2. Pingback: Copying an image to another in ANDROID. | Coderz Heaven | WWW.ANDROIDWORLD.BIZ

  3. Pingback: Copying an image to another in ANDROID. | Coderz Heaven - Mobile Universe Online | Mobile Universe Online

  4. Android Applications Development

    Thanks for discussing in detail about copying of image to another.This is really helpful.

    Reply

Leave a Reply

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