2

私の問題

内部データ ディレクトリのディレクトリに画像を保存するカメラの意図があります。

以下を使用してディレクトリを作成しました。

        File mydir3 = new File(this.getApplicationContext().getFilesDir(),File.separator+"MyApplication"+File.separator+CCPhotoManager.DIRECTORY+File.separator+PhotoManager);
        mydir3.mkdirs();

次に、以下に示すように、指定された名前でファイルを作成します。

       String filePath = this.getApplicationContext().getFilesDir()+File.separator+"MyApplication"+File.separator+PhotoManager+File.separator+PhotoManager().getNewPhotoFileName()+PhotoManager;

        File file = new File(filePath);

次に、画像が保存されない理由は、カメラがアプリケーションのデータ ディレクトリにアクセスする権限を持っていないためでありFileOutputStream、権限を変更するために使用する必要があることをどこかで読みました。ただし、ファイルのパスにパス区切り文字があるため、使用できませんでしopenFileStream(filePath,Context.MODE_WORLD_WRITABLE);た。

これが私が以下を試した理由です。

       try {
            FileOutputStream fos = new FileOutputStream(file);
            fos.close();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

ただし、以下のコードを使用して作成されたファイルに画像が保存されないため、これは明らかに機能しません。

        Uri uriSavedImage=Uri.fromFile(file);
        Log.d("PhotoManager", "adding extra to intent: "+uriSavedImage.toString());
        Intent launchcameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        launchcameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);
        startActivityForResult(launchcameraIntent,CAMERA_PIC_REQUEST);

私の質問

内部データのカスタム ディレクトリに画像を保存するカメラ インテントを取得するにはどうすればよいですか?

前もって感謝します

4

2 に答える 2

1

I have this same problem once and I solved it with this:

1.Create a file from a image that exists in a default folder as:

File from = new File("path to default file created");

Here in your code path is described as filepath.

2.Create another File where you want the file to save as:

File to = new File("target file path");

3.Rename the file as:

from.renameTo(to);

With this the file from default path is automatically deleted and created at the new path.

thanks

于 2012-10-16T11:41:21.260 に答える
1

これが私がそれを解決した方法です。@AppMobiGurmeet にも感謝します。

以下のように、写真を SD カードのファイルに保存します。

        File directory= new File(Environment.getExternalStorageDirectory()+File.separator+"MyApplication"+File.separator+"TemporaryImages");//+File.separator+CCPhotoManager.getInstance().getNewPhotoFileName()+CCPhotoManager.JPEG_FILE_SUFFIX);
        directory.mkdirs();

        File externalFile = new File(Environment.getExternalStorageDirectory()+File.separator+"MyApplication"+File.separator+"TemporaryImages"+File.separator+CCPhotoManager.getInstance().getNewPhotoFileName()+CCPhotoManager.JPEG_FILE_SUFFIX);

        Uri uriSavedImage=Uri.fromFile(externalFile);
        Log.d("PhotoManager", "adding extra to intent: "+uriSavedImage.toString());
        Intent launchcameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        launchcameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);
        startActivityForResult(launchcameraIntent,CAMERA_PIC_REQUEST);

次に、活動結果について。(インテントが返されたとき)

            // SAVE THE PHOTO TO THE INTERNAL DATA DIRECTORY
            File to = new File(MyApplication.getAppContext().getFilesDir()+File.separator+"MyApplication"+File.separator+DIRECTORY+File.separator+this._currentPhotoName+JPEG_FILE_SUFFIX);
            File from = new File(Environment.getExternalStorageDirectory()+File.separator+"MyApplication"+File.separator+"TemporaryImages"+File.separator+this._currentPhotoName+JPEG_FILE_SUFFIX);

            try {
                FileChannel destination = new FileOutputStream(to).getChannel();
                FileChannel source = new FileInputStream(from).getChannel();
                if (destination != null && source != null) {
                    destination.transferFrom(source,0,source.size());
                    from.delete();
                }
                destination.close();
                source.close();
            } catch (FileNotFoundException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

しかし、このようにいじった後、一部のデバイスでは内部スペースが非常に少ないため、内部データディレクトリは画像やファイルなどを保存するのに最適な場所ではないと判断しました(Samsung Galaxy Ace約200MB)。

これが誰かに役立つことを願っています。

于 2012-10-16T14:34:13.753 に答える