进来一段时间一直在弄系统开发,短信原生相关的,那天测试突然打过来一问题,图片分享报错,第一反应就是什么鬼,这测试瞎几把测出什么鬼问题,一看详细步骤描述,原来是图片通过Google相册打开分享到短信,出现未知错误的提示。
图片分享短信,肯定是Intent调用短信,该意图里面包含图片uri等一堆参数,出现该问题要么uri有问题,要么短信里面处理有问题,短信里面的处理逻辑不太清楚,估计原生的出错几率不大,先从uri入手,抓手日志看下带过来的uri到底是什么
如下显示
content://com.google.android.apps.photos.contentprovider/-1/1/file%3A%2F%2F%2Fstorage%2Femulated%2F0%2FDCIM%2FCamera%2FIMG_20160101_194209.jpg/ORIGINAL/NONE/1397485324
容易看出是从google相册provider生成的uri,前面跟后面都没什么用,关键是把这一段file%3A%2F%2F%2Fstorage%2Femulated%2F0%2FDCIM%2FCamera%2FIMG_20160101_194209.jpg截取出来交给短信去处理就行了,而短信中是有处理外部调用逻辑的代码,但貌似没有处理这种格式的代码,所以我们就需中转一下,在处理逻辑中判断一下,如果是来自google相册的调用,就把uri处理后再往下执行,这样就不会报错了。
首先,定义个静态常量,用来判断google相册
//google photos
private static final String GOOGLE_APP_PHOTO = "content://com.google.android.apps."
+ "photos.contentprovider";
接下来就是写个处理uri的方法用于调用
//add google photos
private Uri handleGooglePhoto(Uri uriExtra) {
Uri newUri = null;
try {
String unusablePath = uriExtra.getPath();
int startIndex = unusablePath.indexOf("external/");
Log.d("lx", "startIndex:" + startIndex);
if (startIndex < 0) {
String encodingPath = uriExtra.getEncodedPath();
Log.d("lx", "encodingPath:" + encodingPath);
//该处匹配的后缀因机型不同所生成的后缀也不尽相同,需要自行抓取日志查看
int endIndex = encodingPath.indexOf("/ORIGINAL");
if (endIndex < 0) {
Log.e(TAG, "Google Path URI = " + uriExtra.toString());
return null;
}
int start = encodingPath.lastIndexOf("/", endIndex - 1);
Log.d("lx", "start:" + start + " endIndex= " + endIndex);
if (start < 0) {
Log.e(TAG, "Google Path URI = " + uriExtra.toString());
return null;
}
String embeddedPath = encodingPath.substring(start + 1, endIndex);
Log.d("lx", "embeddedPath:" + embeddedPath);
if (embeddedPath == null || embeddedPath.length() == 0) {
Log.e(TAG, "Google Path, Null Path, URI = " + uriExtra.toString());
return null;
}
String newPath = URLDecoder.decode(embeddedPath, "UTF-8");
newUri = Uri.parse(newPath);
} else {
int endIndex = unusablePath.indexOf("/ORIGINAL");
if (endIndex < 0) {
Log.e(TAG, "Google Path URI = " + uriExtra.toString());
return null;
}
String embeddedPath = unusablePath.substring(startIndex, endIndex);
Log.d("lx", "endIndex:" + endIndex + " embeddedPath= " + embeddedPath);
Uri.Builder builder = uriExtra.buildUpon();
builder.path(embeddedPath);
builder.authority("media");
newUri = builder.build();
}
Log.d(TAG, "newUri:" + newUri);
} catch (IndexOutOfBoundsException e) {
Log.d(TAG, "handleGooglePhoto(), IndexOutOffBound");
} catch (NullPointerException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return newUri;
}
找到短信中处理外部调用的地方,调用上述方法,主要处理逻辑在这个方法里
// Handle send actions, where we're told to send a picture(s) or text.
private boolean handleSendIntent() {
.............
.......................
String scheme = tempUri.getScheme();
if (scheme != null && scheme.equals("file")) {
/// M: fix bug ALPS01400468, workaround for GoogleDrive Audio Uri
if (uri.toString().contains(AudioModel.sAuthorityForGoogleDrive)) {
AudioModel.sTypeForGoogleDrive = mimeType;
}
addFileAttachment(mimeType, tempUri, false);
} else {
//此处拦截
//add google photos
if (tempUri.toString().startsWith(GOOGLE_APP_PHOTO)){
Uri newUrl = handleGooglePhoto(tempUri);
if (newUrl != null) {
tempUri = newUrl;
}
}
addAttachment(mimeType, tempUri, false);
}
..............
if (scheme != null && scheme.equals("file")) {
// change "file://..." Uri to "Content://...., and attemp to add this attachment"
/// M: fix bug ALPS604911, change MmsContentType when share multi-file from FileManager @{
String type = MessageUtils.getContentType((Uri) uri);
if (TextUtils.isEmpty(type)) {
type = mimeType;
}
/// @}
addFileAttachment(type, (Uri) uri, true);
} else if (authority != null && (authority.contains("com.android.email")
|| (authority.equals(MediaStore.AUTHORITY) && mimeType != null && mimeType.contains("*")))) {
addAttachment(mimeType, (Uri) uri, true);
} else {
String type = MessageUtils.queryContentType(ComposeMessageActivity.this, (Uri) uri);
if (TextUtils.isEmpty(type)) {
type = mimeType;
}
//add google photos
//此处也需拦截
addAttachment(type, handleGooglePhoto((Uri) uri), true);
}
}
}
这样,分享图片就没问题了,能正常生成彩信
对于原生短信应用,我也正在研究中,有兴趣的朋友可以一起交流。