iApp调用java实现图片选择,并返回图片路径

1.项目结构,如图:


iApp调用java实现图片选择,并返回图片路径_第1张图片
图片发自App
iApp调用java实现图片选择,并返回图片路径_第2张图片
图片发自App

2.实现代码:

//image.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;
import android.os.Environment;

public static String getPath(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 ("com.android.externalstorage.documents".equals(uri.getAuthority()))
    {
      String[] split = docId.split(":");
      String type = split[0];
      if ("primary".equalsIgnoreCase(type)) 
      {
        imagePath = Environment.getExternalStorageDirectory() + "/" + split[1];
      }
    }
  }
  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;
}
//选择图片,单击事件代码

javags(agc,"android.content.Intent","android.content.Intent.ACTION_GET_CONTENT")
javanew(it,"android.content.Intent","String",agc)

java(c,it,"android.content.Intent.setType","String","image/*")

javags(ca,"android.content.Intent","android.content.Intent.CATEGORY_OPENABLE")
java(c,it,"android.content.Intent.addCategory","String",ca)

java(c,activity,"android.app.Activity.startActivityForResult","android.content.Intent",it,"int",2)

3.设置回调事件


iApp调用java实现图片选择,并返回图片路径_第3张图片
图片发自App
//回调事件代码
javags(er,activity,"android.app.Activity","RESULT_OK")
f(st_lC==er)
{
  f(st_sC==2)
  {
    java(uri,st_iT,"android.content.Intent.getData")
    call(path, "mjava", "image.getPath", activity, uri)
    syso(path)
    us(2, "src", path)
    us(3, "text", path)
  }
}

你可能感兴趣的:(iApp调用java实现图片选择,并返回图片路径)