将用户图库中的图像复制到SDCard上的文件夹

时间:2013-10-02 20:05:17

标签: android image copy gallery sd-card

首先让我说我是编程新手。我正在尝试创建一个私人图库,我已经在网上搜索并得到了一些零碎的东西来创建这个应用程序

我有一个基本的画廊,它显示我在网格中的私人文件夹中的图像,我可以选择一个图像,用捏放大,放大和缩小所有这些好东西。

我现在要做的是......当用户启动他们的普通图库并选择图像(或视频),然后选择SHARE选项时,他们选择我的应用程序。我希望它将文件复制到我的私人位置“/DCIM/privgal/.nomedia/”,然后从普通图库中删除该文件。

我正在使用HTC ONE进行所有测试,当我从“共享”菜单中选择我的应用程序时,该库崩溃并希望将报告发送给HTC。我在LogCat中没有看到任何错误,就好像它从未实际调用我的应用程序所以我看不出有什么问题。

我知道下面这段代码是乱七八糟的,我知道它不是正常的功能,但正如我之前所说的,我是新手,并将这些零碎的东西聚集在一起,并希望让它能够处理日志中的错误猫会给我。不幸的是,它没有报告任何错误,所以我被困住了。

有人可以看看这个并且要么指向一个工作示例的方向,要么......我真的不想说出来,修复我的代码?

任何帮助表示赞赏!!!

-Steve

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

2 个答案:

答案 0 :(得分:2)

以下是工作代码,使用图库中的“发送到/共享菜单”将图像复制到存储上的预定义隐藏文件夹,并从用户图库中删除文件

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();

        }

    }

在Manafest中添加了活动和权限

权限:

    <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 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>

答案 1 :(得分:1)

活动只有私有方法,没有onCreate覆盖方法,所以没有调用任何方法。实际上,活动什么都不做,没有视图,因此应用程序根本不起作用。

您需要覆盖onCreate方法,使用getInvent显示,获取意图,然后使用getData获取内容,等等。