繁体   English   中英

无法在Android中使用相机创建文件拍摄照片

[英]Can't create file to take Picture using Camera in Android

我正在使用以下代码,并且我不想将图像存储在SDCard中。 我GET异常,找不到文件或目录。 请给我一个提示我错了!@!

  case 0:
        Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        try {
           photo = this.createTemporaryFile("Temp", ".jpg");
           intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));
        } catch(Exception e) {
           Log.v("Error", "Can't create file to take picture!");
           break;
        }
        startActivityForResult(intent, RESULT_CAMERA_SELECT);
        break;
  case 1: 
        intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        startActivityForResult( intent, RESULT_MEMORY_SELECT );
        break;
  default:
        break;
        }
   }


    private File createTemporaryFile(String part, String ext) throws Exception
    {
        File tempDir= Environment.getRootDirectory();
        tempDir=new File(tempDir.getAbsolutePath()+"/.temp/");
        if(!tempDir.exists())
        {
            tempDir.mkdir();
        }
        return File.createTempFile(part, ext, tempDir);
    }

最好的祝福

确保您已授予写入外部存储的权限。

以及如何以这种方式创建文件:

String filePath= Environment.getExternalStorageDirectory()+"/myPic.jpeg";
File file=new File(filePath);
Uri output=Uri.fromFile(file);
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, output);
startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);

该代码对我有用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM