【解决】Caused by: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=99

Problem scene

This situation occurs when the Android program saves the photos taken by the camera to the mobile phone. The main reason is that the data returned by the operation of storing photos is empty, and there is no reason for reasonable handling in the code. When using the APP, there is a flashback phenomenon. Based on this, the article analyzes and solves the problem.

The problematic code

Camera photo request code:

REQUEST_CODE_CAMERA = 1
//实例化一个intent,并指定action
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
//指定一个图片路径对应的file对象 
uri = Uri.fromFile(ImageUtil.getImageFile());
// 将所拍照片写至 uri 对应的文件路径
intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
//启动activity
startActivityForResult(intent, REQUEST_CODE_CAMERA);

The result processing code after taking pictures:

protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    
    
        super.onActivityResult(requestCode, resultCode, data);
        //此处直接认为请求反馈回来的有数据,进行后续处理了。
		 if(requestCode == 1) {
    
    
                if(resultCode == RESULT_OK) {
    
    
                    Toast.makeText(getApplicationContext(), "图片已保存",
                           Toast.LENGTH_SHORT).show(); 
                }           
           }
       } 

Cause Analysis

1 First look at the operating logic of the Android program

The following code shows the key code in the process of taking pictures with the Android camera. It can be seen from the code logic: By
default, the Android system framework Camera application does not need to specify intent.putExtra(MediaStore.EXTRA_OUTPUT, uri); the camera has its own default The storage path, the photos taken will be written to the corresponding path, and only a response code RESULT_OK will be returned. If you want to access the original image, you can get the location of the original image through getIntent().getStringExtra(MediaStore.EXTRA_OUTPUT), and then access it picture. If the target uri is specified, data will have no feedback data, if no uri is specified, data will return data, and it is a bitmap!
In summary, the cause of the error may be that we did not perform null value judgment on the data in the onActivityResult method.

// 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) {
    
    	//存在mSaveUri,即指定了目标uri
	OutputStream outputStream = null;
	try {
    
    
		outputStream = mContentResolver.openOutputStream(mSaveUri);
		outputStream.write(data);
		outputStream.close();
 
		setResult(RESULT_OK);	//直接返回RESULT_OK,并没有指定intent
		finish();
	} catch (IOException ex) {
    
    
		// ignore exception
	} finally {
    
    
		Util.closeSilently(outputStream);
	}
} else {
    
    
	Bitmap bitmap = createCaptureBitmap(data);
	// 返回RESULT_OK,并包含一个Intent对象,其中Extra中科key为data,value为一个bitmap
	setResult(RESULT_OK, new Intent("inline-data").putExtra("data", bitmap));
	finish();
}

other reasons

Problem 1
Our custom storage path does not exist, and we did not determine whether the path exists before saving, to create a new path, resulting in photos that cannot be stored. For example, the custom storage path is /mnt/sdcard/pics/, and the phone SD card If there is no
folder named pics before taking pictures, some mobile phones will not save the pictures after taking pictures, so we cannot get photos. Most mobile phones’ cameras will create folders that do not exist when the folder does not exist. , 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, and if they do not exist, create them first and then call the camera.
Question 2
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 spaces in the path with "%20". In fact, this is not a problem for most mobile phones. When the mobile phone parses the storage path, it will replace "%20" with a space. In fact, the final photo name is still the name we specified at the beginning: 123 1.jpg, unfortunately It is because the built-in camera 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" and cannot find the image.

According to the above analysis, the corresponding solutions are as follows:

(1) If we specify a path, don't use data to get photos, at least make a short judgment before using it, that is, use the (data) returned in onActivityResult to make a short judgment.
(2) When specifying a photo path, first check whether all the folders in the path exist, and if they do not exist, create a folder first and then call the camera to take pictures.
(3) When specifying the photo storage path, do not include special symbols such as spaces in the photo name.

[The article draws on the article of this predecessor: https://blog.csdn.net/zimo2013/article/details/16916279]

Guess you like

Origin blog.csdn.net/qq_29750461/article/details/131740444