Consolidating gallery images in android using custom share actions

The other day I was trying to find a simple way to copy all my photos from the gallery on my android phone to a separate folder so I could easily back them up. This is not always an easy task, given that images in the gallery can often be located in many different directories. I wasn’t satisfied with the current options presented to me (bluetooth, email, facebook etc.) so I decided to write a simple application that handles a custom share action.

First, create a new android project in eclipse and add the WRITE_EXTERNAL_STORAGE permission as well as adding an intent action for your main intent for the SEND and SEND_MULTIPLE actions to your android manifest file. When done, your android manifest file should look like this:


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

<uses-sdk android:minSdkVersion="7" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".CustomShareActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEND" />
<action android:name="android.intent.action.SEND_MULTIPLE" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="image/*" />
</intent-filter>

</activity>
</application>

</manifest>

I then added the following code as my main source file for the project:


package com.wordpress.danielbeard;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.os.*;

import org.apache.commons.io.*;

public class CustomShareActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

Intent theIntent = getIntent();
String theAction = theIntent.getAction();
if (Intent.ACTION_SEND.equals(theAction)) {

Bundle bundle = getIntent().getExtras();
Uri i = (Uri)bundle.get("android.intent.extra.STREAM");

String inputfilename = getRealPathFromURI(i);
String outputfilename = getFileNameFromPath(getRealPathFromURI(i));

File inputFile = new File(inputfilename);
File outputFolder = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "PhotoZipper");
File outputFile = new File(outputFolder.toString() + File.separator + outputfilename);

//create the directory if it doesn't exist
outputFolder.mkdir();

Log.e("photomover", inputFile.toString());
Log.e("photomover", outputFile.toString());

//try to copy the file
try {
FileUtils.copyFile(inputFile, outputFile);
}
catch (IOException e) {
Log.e("photomover", e.toString());
}
}

//send multiple
if (Intent.ACTION_SEND_MULTIPLE.equals(theAction) && theIntent.hasExtra(Intent.EXTRA_STREAM)) {

ArrayList list = theIntent.getParcelableArrayListExtra(Intent.EXTRA_STREAM);
for (Object p : list)
{
Uri uri = (Uri) p; /// do something with it.
Log.e("photomover",uri.toString());

//Create input and output paths
String inputfilename = getRealPathFromURI(uri);
String outputfilename = getFileNameFromPath(getRealPathFromURI(uri));

//Create files with the input/output paths
File inputFile = new File(inputfilename);
File outputFolder = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "PhotoZipper");
File outputFile = new File(outputFolder.toString() + File.separator + outputfilename);

//create the directory if it doesn't exist
outputFolder.mkdir();

//Log the result
Log.e("photomover", inputFile.toString());
Log.e("photomover", outputFile.toString());

//try to copy the file
try {
FileUtils.copyFile(inputFile, outputFile);
}
catch (IOException e) {
Log.e("photomover", e.toString());
}
}
}
}

public String getFileNameFromPath(String input) {
String[] temp;
String delimeter = "/";
temp = input.split(delimeter);
return temp[temp.length - 1];
}

// Convert the image URI to the direct file system path of the image file
public String getRealPathFromURI(Uri contentUri) {

String [] proj={MediaStore.Images.Media.DATA};
Cursor cursor = managedQuery( contentUri,
proj, // Which columns to return
null,       // WHERE clause; which rows to return (all rows)
null,       // WHERE clause selection arguments (none)
null); // Order-by clause (ascending by name)
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();

return cursor.getString(column_index);
}
}

Note: I am using the apache commons IO library to make copying files a bit easier.
That’s it! Now when I select multiple files in the gallery, I can consolidate them easily into a single folder by pressing share > PhotoMover.

Advertisements

5 thoughts on “Consolidating gallery images in android using custom share actions

  1. is onCreate called every time the action is used? for example if I want to show a file dialog every time the action is selected is this the place to do it…?

  2. Great blog! Do you have any recommendations for aspiring writers?
    I’m hoping to start my own website soon but I’m a little lost
    on everything. Would you recommend starting with a free
    platform like WordPress or go for a paid option?
    There are so many choices out there that I’m completely confused .. Any suggestions? Cheers!

  3. Good day! I simply would like to give you a huge thumbs up for the great info you have here on this
    post. I am returning to your blog for more soon.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s