Sobo
Sobo

Reputation: 497

Copy Image from Users Gallery to folder on SDCard

Let me start off by saying I am new to programming. I am trying to create a Private Gallery and I have searched the web and gotten bits and pieces together to create this app

I have the basic gallery down and it displays the images that I have in my private folder in a grid and I can pick an image to enlarge it with pinch to zoom in and out and all those good things.

What I want to do now is... When the users starts their normal gallery and selects an image (or video) and then selects the SHARE Option, they Pick my App. I want it to Copy the file to my Private Location "/DCIM/privgal/.nomedia/" and then delete the file from their Normal Gallery.

I am using an HTC ONE at the moment for all my testing and when I select my App from the Share menu, the gallery crashes and wants to send the report to HTC. I do not see any errors in my LogCat, its as if it never actually calls my App so I can't see what's wrong.

I know this code below is a mess and I know it's not functional as is, but as I said before I am new to this and gathered these bits and pieces together and was hoping to get it working with the errors that the log cat would give me. Unfortunately it's not reporting any errors, so I am stuck.

Could someone PLEASE look at this and either point me in a direction to a working example or... I really hate to say it, fix my code?

Any help is appreciated!!!

-Steve

Working Code Below posted by Me. (see Below 10/27/2013)

Upvotes: 0

Views: 5785

Answers (2)

Sobo
Sobo

Reputation: 497

Here is Working Code to Use the "Send To/Share Menu" from the Gallery to Copy an image to a predefined Hidden folder on the Storage and delete the file from the Users Gallery

SendToActivity.java

package com.company.privategallery;

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.Gravity;
import android.view.KeyEvent;
import android.widget.Toast;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

 public class SendToActivity extends Activity {

        private static final int SELECT_PICTURE = 0;
        //Generating Random Number to use as a unique file name
        static int random = (int)Math.ceil(Math.random()*100000000);
        private static String fname = Integer.toString(random);

        private static String selectedImagePath;
        //Getting the external Path to the Storage
        private static String ExternalStorageDirectoryPath = Environment.getExternalStorageDirectory().getAbsolutePath();
        //Setting a directory that is already created on the Storage to copy the file to.
        private static String targetPath = ExternalStorageDirectoryPath + "/DCIM/privgal/.nomedia/";

        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            //get the received intent
            Intent receivedIntent = getIntent();

            //get the action
            String receivedType = receivedIntent.getType();

            //make sure it's an action and type we can handle
                if(receivedType.startsWith("image/")){
                    //get the uri of the received image
                    Uri receivedUri = (Uri)receivedIntent.getParcelableExtra(Intent.EXTRA_STREAM);
                    selectedImagePath = getPath(receivedUri);
                    System.out.println("Image Path : " + selectedImagePath);
                    //check we have a uri
                    if (receivedUri != null) {
                        //Copy the picture
                            try {
                                copyFile(selectedImagePath, targetPath + fname + ".jpg");
                            } catch (IOException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
                            startGallery();
                            deleteFile();
                            onDestroy();
                    }
                }   
        }

        public void copyFile(String selectedImagePath, String string) throws IOException {
            InputStream in = new FileInputStream(selectedImagePath);
            OutputStream out = new FileOutputStream(string);

            // Transfer bytes from in to out
            byte[] buf = new byte[1024];
            int len;
            while ((len = in.read(buf)) > 0) {
                out.write(buf, 0, len);
            }
            in.close();
            out.close();
            Toast customToast = new Toast(getBaseContext());
            customToast = Toast.makeText(getBaseContext(), "Image Transferred", Toast.LENGTH_LONG);
            customToast.setGravity(Gravity.CENTER|Gravity.CENTER, 0, 0);
            customToast.show();
          }

        private void startGallery() {
            Intent intent = new Intent();
            intent.setType("image/*");
            intent.setAction(Intent.ACTION_GET_CONTENT);
            intent.addCategory(Intent.CATEGORY_OPENABLE);
            startActivityForResult(intent, SELECT_PICTURE);
        }

        // Delete the file that was copied over
        private void deleteFile() {
            File fileToDelete =  new File(selectedImagePath);
            boolean fileDeleted =  fileToDelete.delete();

            // request scan    
            Intent scanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
            scanIntent.setData(Uri.fromFile(fileToDelete));
            sendBroadcast(scanIntent);
            sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" +  Environment.getExternalStorageDirectory())));
            finish();
        }

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

        @Override
        public void onDestroy() {
            super.onDestroy();
            android.os.Process.killProcess(android.os.Process.myPid());
            finish();

        }

    }

Added Activity and Permissions in Manafest

Permissions:

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

Activity:

    <activity android:name="com.company.privategallery.SendToActivity" 
        android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.SEND" />
                <category android:name="android.intent.category.DEFAULT" />
                <data android:mimeType="image/*" />
            </intent-filter>
    </activity>

Upvotes: 2

Maxim Efimov
Maxim Efimov

Reputation: 2737

The activity has only private methods, no onCreate overriden method, so none of the methods is called. Actually the activity does nothing, has no view and thus application is not working at all.

You need to override onCreate method, use getInvent for, obviuosly, get the intent and then getData for the content and so on.

Upvotes: 1

Related Questions