Android ACTION_IMAGE_CAPTURE Intent

后端 未结 14 2016
被撕碎了的回忆
被撕碎了的回忆 2020-11-22 02:04

We are trying to use the native camera app to let the user take a new picture. It works just fine if we leave out the EXTRA_OUTPUT extra and returns the small B

14条回答
  •  青春惊慌失措
    2020-11-22 03:01

    The file needs be writable by the camera, as Praveen pointed out.

    In my usage I wanted to store the file in internal storage. I did this with:

    Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    if (i.resolveActivity(getPackageManager()!=null)){
        try{
            cacheFile = createTempFile("img",".jpg",getCacheDir());
            cacheFile.setWritavle(true,false);
        }catch(IOException e){}
        if(cacheFile != null){
            i.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(cacheFile));
            startActivityForResult(i,REQUEST_IMAGE_CAPTURE);
        }
    }
    

    Here cacheFile is a global file used to refer to the file which is written. Then in the result method the returned intent is null. Then the method for processing the intent looks like:

    protected void onActivityResult(int requestCode,int resultCode,Intent data){
        if(requestCode != RESULT_OK){
            return;
        }
        if(requestCode == REQUEST_IMAGE_CAPTURE){
            try{
                File output = getImageFile();
                if(output != null && cacheFile != null){
                    copyFile(cacheFile,output);
    
                    //Process image file stored at output
    
                    cacheFile.delete();
                    cacheFile=null;
                }
            }catch(IOException e){}
        }
    }
    

    Here getImageFile() is a utility method to name and create the file in which the image should be stored, and copyFile() is a method to copy a file.

提交回复
热议问题