android - 将相机 Intent 图像保存到内部数据自定义目录

标签 android android-camera android-camera-intent

我的问题

我有一个相机 Intent ,我希望将图像保存到我的内部数据目录中的目录中。

我使用以下方法创建了目录:

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

我的问题

如何让相机 Intent 将其图像保存到内部数据中的自定义目录?

提前致谢

最佳答案

我曾经遇到过同样的问题,我用这个解决了它:

1.从默认文件夹中存在的图像创建文件:

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

这里您的代码路径被描述为文件路径。

2.创建另一个文件,您希望文件另存为:

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

3.将文件重命名为:

from.renameTo(to);

这样,默认路径中的文件将自动删除并在新路径中创建。

谢谢

关于android - 将相机 Intent 图像保存到内部数据自定义目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12913842/

相关文章:

Android Camera.Parameters setPictureSize 不起作用

android-camera - 使用相机拍摄照片时数据为空 (Nexus 7)

Android相机 Intent

android - 如何从 Android 执行 RESTful POST 请求

android - IBM Worklight - 推送通知功能在 Android 模拟器中不起作用

java - 为什么 addIntentsToList 方法给出错误 'Cannot resolve the method'

Android 相机照片返回 null

android - 无法访问 sys_call_table

android notifydatasetchange()不工作

android - OnPreviewFrame数据图片到imageView