新建uri.mjava
import android.content.ContentUris;
import android.content.Intent;
import android.net.Uri;
import android.provider.DocumentsContract;
import android.provider.MediaStore;
import android.database.Cursor;
import android.content.Context;
public static String getRealPath(Context context, Uri uri)
{
String imagePath = null;
if(DocumentsContract.isDocumentUri(context, uri))
{
// 如果是document类型的Uri,则通过document id处理
String docId=DocumentsContract.getDocumentId(uri);
if ("com.android.providers.media.documents".equals(uri.getAuthority()))
{
String id = docId.split(":")[1];
String selection = MediaStore.Images.Media._ID + "=" + id;
imagePath = getImagePath(context, MediaStore.Images.Media.EXTERNAL_CONTENT_URI, selection);
}
else if ("com.android.providers.downloads.documents".equals(uri.getAuthority()))
{
Uri contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"), Long.valueOf(docId));
imagePath = getImagePath(context, contentUri, null);
}
}
else if("content".equalsIgnoreCase(uri.getScheme()))
{
// content类型普通方式处理
imagePath = getImagePath(context, uri, null);
}
else if("file".equalsIgnoreCase(uri.getScheme()))
{
// file类型直接获取图片路径
imagePath = uri.getPath();
}
return imagePath;
}
public String getImagePath(Context context, Uri uri, String selection)
{
String path = null;
// 通过Uri和selection来获取真实的图片路径
Cursor cursor = context.getContentResolver().query(uri, null, selection, null, null);
if (cursor != null)
{
//syso(cursor.moveToFirst());
if (cursor.moveToFirst())
{
path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
}
cursor.close();
}
return path;
}
//iyu
javanew(intent,"android.content.Intent","String","android.intent.action.GET_CONTENT")
java(setType,intent,"android.content.tent.setType","String","image/*")
java(null,activity,"android.app.Activity.startActivityForResult","android.content.Intent",intent,"int",2)
//回调结果事件
javags(resultCode,activity,"android.app.Activity","RESULT_OK")
f(st_sC==2)
{
f(st_lC==resultCode)
{
java(uri,st_iT,"android.content.Intent.getData")
call(path, "mjava", "uri.getRealPath", activity, uri)
}
}
网友评论