0

私はプログラミングが初めてだと言って始めましょう。プライベート ギャラリーを作成しようとしています。このアプリを作成するために Web を検索し、断片をまとめました。

基本的なギャラリーがダウンしており、プライベートフォルダーにある画像がグリッドに表示され、画像を選択してピンチで拡大およびズームアウトすることができます。

私が今やりたいことは... ユーザーが通常のギャラリーを開始して画像 (またはビデオ) を選択し、次に共有オプションを選択すると、アプリを選択します。ファイルを自分のプライベート ロケーション「/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 に答える