Saving Camera Intent Image To Internal Data Custom Directory

我只是一个虾纸丫 提交于 2019-12-08 03:13:40

问题


My Problem

I have a camera intent which I want the image to be saved to a directory in my internal data directory.

I have created the directory using:

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

I then proceed to create the file with the specified name as you can see below.

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

        File file = new File(filePath);

Then I read somewhere that the reason the image isn't being saved is because the camera doesn't have permission to access my applications data directory and that you need to use FileOutputStream to change the permission. However, due to my file's path having path separators I couldn't use openFileStream(filePath,Context.MODE_WORLD_WRITABLE);.

This is why I tried the below.

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

However this obviously doesn't work because no image is saved to the file created using the code below.

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

My Question

How do I get a camera intent to save its image to my custom directory in my internal data?

Thanks in advance


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




回答2:


So this is how I solved it. Thanks goes to @AppMobiGurmeet also.

Save the photo to a file on the SDCard like below.

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

Then on activity result. (when the intent is returned)

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

I have now decided however, after all this faffing around, that the internal data directory is not the best place to store things like Images and Files due to, on some devices, there being a very small amount of internal space (Samsung Galaxy Ace approx 200MB).

I hope this helps someone.



来源:https://stackoverflow.com/questions/12913842/saving-camera-intent-image-to-internal-data-custom-directory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!