美文网首页iApp新手导师[正官方]
iApp教学||iapp利用java调用图库

iApp教学||iapp利用java调用图库

作者: 醉笑ya | 来源:发表于2020-08-23 16:16 被阅读0次


    新建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)

    }

    }


    相关文章

      网友评论

        本文标题:iApp教学||iapp利用java调用图库

        本文链接:https://www.haomeiwen.com/subject/nhtwjktx.html