-->

保存相机图像意向内部数据自定义目录(Saving Camera Intent Image To In

2019-09-24 04:55发布

我的问题

我有我想要的形象在我的内部数据目录下的一个目录的相机意图。

我创建使用目录:

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

我的问题

我如何获得一个摄像头意图的图像保存到我的自定义目录在我的内部数据?

提前致谢

Answer 1:

我有同样的问题一次,我这个解决它:

1.创建从存在于默认文件夹的图片文件:

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

在这里,你的代码路径被描述为文件路径。

2.创建您想要的文件作为保存另一个文件:

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

3.Rename的文件:

from.renameTo(to);

有了这个从默认的路径中的文件被自动删除,在新的路径中创建。

谢谢



Answer 2:

所以这是我如何解决它。 由于去也@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();
            }

现在我已经然而决定,这一切faffing四周后,将内部数据目录不是存储之类的图像和文件因,在某些设备上,有作为的内部空间非常小的量(三星Galaxy王牌约的最佳场所200MB)。

我希望这可以帮助别人。



文章来源: Saving Camera Intent Image To Internal Data Custom Directory