android - 使用相机 Intent 拍照并保存到文件

标签 android file camera save

<分区>

Possible Duplicate:
Android Camera - Save image into a new folder in SD Card

我正在尝试拍照并将其保存到文件中。问题来了,我正在尝试将位图保存到文件中。这是我的代码:

private void takePic() {
    Intent cameraIntent = new Intent(
            android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    startActivityForResult(cameraIntent, 2);


}

public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 2) {
            Bitmap photo = (Bitmap) data.getExtras().get("data");
            ImageView test = (ImageView) findViewById(R.id.test);
            test.setImageBitmap(photo);

            try {
                FileOutputStream out = new FileOutputStream("filename");
                photo.compress(Bitmap.CompressFormat.JPEG, 90, out);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

以及 logcat 中的异常:

04-02 14:46:51.975: W/IInputConnectionWrapper(2225): showStatusIcon on inactive InputConnection
04-02 14:46:56.135: W/System.err(2225): java.io.FileNotFoundException: /filename (Read-only file system)
04-02 14:46:56.135: W/System.err(2225):     at org.apache.harmony.luni.platform.OSFileSystem.open(Native Method)
04-02 14:46:56.145: W/System.err(2225):     at dalvik.system.BlockGuard$WrappedFileSystem.open(BlockGuard.java:232)
04-02 14:46:56.145: W/System.err(2225):     at java.io.FileOutputStream.<init>(FileOutputStream.java:94)
04-02 14:46:56.145: W/System.err(2225):     at java.io.FileOutputStream.<init>(FileOutputStream.java:165)
04-02 14:46:56.145: W/System.err(2225):     at java.io.FileOutputStream.<init>(FileOutputStream.java:144)

最佳答案

你的错误清楚地表明 java.io.FileNotFoundException:/filename

请提供准确的路径“/sdcard/filename”

new FileOutputStream(getExternalStorageDirectory()+"filename");

String imageFilePath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/name.png";

注意:在 list 文件中添加权限 WRITE_EXTERNAL_STORAGE。

关于android - 使用相机 Intent 拍照并保存到文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9976817/

相关文章:

java - 相机在未经相机许可的情况下工作

video - 用 flutter 捕捉视频

android - 如何在android map api V2中为标记设置动画?

android - 表格布局上的按钮缩放

android - 如果没有 Google Play 服务,googleMapsApp(项目名称)将无法运行。您的设备不支持哪些

forms - Drupal 7 表单 API : File upload

android - 无法在 ListView Android 中添加标题

C语言 : error: ‘O_DIRECTORY’ undeclared and accessing files in directory and sub-directory(without using opendir())

file - 检查文件是否已经打开

Android:如何返回巨大的字节数组作为 Activity 的结果?