保存相机影像意向内部数据自定义目录 [英] Saving Camera Intent Image To Internal Data Custom Directory

查看:116
本文介绍了保存相机影像意向内部数据自定义目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题

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

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

我已经创建使用目录:

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

然后我读的地方,图像不被保存的原因是因为摄像头不具备权限访问我的应用程序数据目录和你需要使用的FileOutputStream 更改的权限。但是,由于我的文件的路径有路径分隔符我不能使用 openFileStream(文件路径,Context.MODE_WORLD_WRITABLE);

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

然而,这是因为没有图像保存到使用下面的code创建的文件显然是行不通的。

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

我的提问

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

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

在此先感谢

推荐答案

这就是我如何解决它。由于去也@AppMobiGurmeet。

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

照片保存到文件上像下面的SD卡。

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

我现在已经然而决定,毕竟这faffing周围,使内部数据目录不是存储之类的图像和文件所致,在某些设备上,那里是内部空间非常少量的最佳场所(三星银河王牌约200MB)。

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.

这篇关于保存相机影像意向内部数据自定义目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆