0

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

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

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

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

我知道下面的代码是一团糟,我知道它没有按原样运行,但正如我之前所说,我是新手,将这些点点滴滴收集在一起,并希望让它与 log cat 会给出的错误一起工作我。不幸的是,它没有报告任何错误,所以我被卡住了。

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

任何帮助表示赞赏!

-史蒂夫

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

2 回答 2

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>
于 2013-10-27T16:31:43.373 回答
1

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

您需要重写onCreate方法,使用getInventfor,显然,获取意图,然后getData为内容等等。

于 2013-10-03T03:51:27.133 回答