尝试从发送操作中提取文件路径:
<data android:mimeType="*/*" />
这是活动:
Uri contentUri = intent.getParcelableExtra(Intent.EXTRA_STREAM);
if (contentUri != null) {
String pathUri = contentUri.toString();
if (pathUri.contains("file://")) {
path = contentUri.getPath();
}
}
if(path==null) {
if (type.startsWith("image/")) {
path = Constant.getRealPathFromURI(this, (Uri) intent.getParcelableExtra(Intent.EXTRA_STREAM));
} else if (type.startsWith("video/")) {
path = Constant.getRealPathFromURI(this, (Uri) intent.getParcelableExtra(Intent.EXTRA_STREAM));
}
}
if (path == null && intent.getData() != null) {
path = intent.getData().getPath();
}
if (path == null) {
Toast.makeText(getApplicationContext(), "None uri !", Toast.LENGTH_LONG).show();
}
所有设备都不是防弹代码吗?代码有什么问题吗? 是否有任何通用的方法来提取发送动作文件路径而不用担心它的mimetype?
修改
public static String getRealPathFromURI(Context context, Uri contentUri) {
String[] proj = {MediaStore.Images.Media.DATA};
Cursor cursor = context.getContentResolver().query(contentUri, proj, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}