Android_camera Camera_ call system camera returns data is empty

Transferred from http://blog.csdn.net/zimo2013
1. Call the system camera
[java] view plain copy View the code slice on CODE Derive to my code slice
// Instantiate an intent and specify action 
Intent intent = new Intent (MediaStore.ACTION_IMAGE_CAPTURE); 
//Specify a file object corresponding to an image path 
uri = Uri.fromFile(ImageUtil.getImageFile()); 
intent.putExtra(MediaStore.EXTRA_OUTPUT, uri); 
//Start activity 
startActivityForResult(intent, REQUEST_CODE_CAMERA) ; 
But the data obtained in the onActivityResult(int requestCode, int resultCode, Intent data) code is always null?
2. Reason analysis
So I checked the Android system framework Camera application and found out about how the system camera handles the return value data!
By default, there is no need to specify intent.putExtra(MediaStore.EXTRA_OUTPUT, uri); the camera has its own default storage path, and the captured photo will return a thumbnail. If you want to access the original image, you can get the original image location through dat extra. That is, if the target uri is specified, data has no data, and if uri is not specified, data returns with data! Now that I think about it, this design is still very reasonable!
[java] view plain copy view snippet on CODE derived to my snippet
@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    switch (requestCode) { 
    case REQUEST_CODE_CAMERA: 
        if (resultCode == RESULT_OK) { 
            if(data !=null){ //may not have specified intent.putExtra(MediaStore.EXTRA_OUTPUT, uri); 
                //return with thumbnail 
                if(data.hasExtra("data")){ 
                    Bitmap thumbnail = data.getParcelableExtra(" data"); 
                    //The operation after getting the bitmap 
                } 
            }else{ 
                //Because the target uri is specified, it is stored in the target uri, intent.putExtra(MediaStore.EXTRA_OUTPUT, uri); 
                // Through the target uri, find 
                the picture//Scaling processing of the picture 
                //Operation 
            } 
        } 
    } 

3. The key source code of the camera part of the Android system
[java] view plain copy View the code slice on CODE Derived from my code slice
// First handle the no crop case -- just return the value. If the 
// caller specifies a "save uri" then write the data to it's 
// stream. Otherwise, pass back a scaled down version of the bitmap 
// directly in the extras. 
if (mSaveUri != null) { //If mSaveUri exists, the target uri is specified 
    OutputStream outputStream = null; 
    try { 
        outputStream = mContentResolver.openOutputStream(mSaveUri); 
        outputStream.write(data); 
        outputStream.close(); 
 
        setResult(RESULT_OK); //Return directly to RESULT_OK without specifying intent 
        finish(); 
    } catch (IOException ex) { 
        // ignore exception 
    } finally { 
        Util.closeSilently(outputStream); 
    } 
} else { 
    Bitmap bitmap = createCaptureBitmap(data); 
    // Returns RESULT_OK and contains an Intent object, where the Extra key is data, value is a bitmap 
    setResult(RESULT_OK, new Intent("inline-data").putExtra("data", bitmap)); 
    finish(); 

4. Common problems and solutions
If we set the storage path for photos, we are likely to encounter the following three problems:
Problem 1: The data returned in the onActivityResult method is empty (the data shows that the data of 93% of the models is empty). It will be Null, so if we specify the path, don't use data to get the photo, at least make an empty judgment before using it)
Problem 2: The photo cannot be stored, if the custom storage path is /mnt/sdcard/lowry/, and There is no folder named lowry in the SD card of the mobile phone before taking pictures, so some mobile phones will not save the pictures after taking pictures, so that we cannot get pictures. Most mobile phone cameras will create a folder that does not exist by itself when the folder does not exist. Folder, but some mobile phones will not be created, the representative models are: Samsung I8258, Huawei H30-T00, Redmi, etc. The solution is to determine whether all the folders in the path exist before specifying the storage path. If they do not exist, create them first and then call the camera.
Question 3: The photo can be stored, but the name is wrong
file:///mnt/sdcard/123 1.jpg, because the fromFile method of Uri will replace the space in the path with "%20". In fact, this is not a problem for most mobile phones. The mobile phone will replace "%20" with a space when parsing the storage path, so in fact, the final photo name is still the name we originally specified: 123 1.jpg, unfortunately The camera that comes with the system of some mobile phones (such as Coolpad 7260) does not read "%20" as a space, the name of the photo after taking the photo is 123%201.jpg, we use the path "file:///mnt/sdcard/123" 1.jpg", it's no wonder that you can find photos! !

Solution:
(1) Make a short judgment before using intent(data) in onActivityResult.
(2) When specifying the photo path, first check whether all the folders in the path exist. If they do not exist, first create a folder and then call the camera to take a photo.
(3) When specifying the photo storage path, do not include special symbols such as spaces in the name of the photo.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326696314&siteId=291194637