Unity Android通过拍照和相册上传头像(图片)

在这里插入图片描述

Unity点击按钮调用AS中显示对话框选择是从相册中上传还是拍照上传,如果是相册直接获得路径传给Unity使用,如果是拍照,先拍照保存在本地,再将路径传递给Unity使用

Android中提供了Intent机制来协助应用间的交互与通讯,Intent负责对应用中一次操作的动作、动作涉及数据、附加数据进行描述,Android则根据此Intent的描述,负责找到对应的组件,将 Intent传递给调用的组件,并完成组件的调用。
Intent对象可以封装传递下面6种信息:

  1. 组件名称(ComponentName)
  2. 动作(Action)
  3. 种类(Category)
  4. 数据(Data)
  5. 附件信息(Extra)
  6. 标志(Flag)

创建选择框

AlertDialog.Builder choiceBuilder = new AlertDialog.Builder(this);
choiceBuilder.setCancelable(false);
choiceBuilder
        .setTitle("选择图片")
        .setSingleChoiceItems(new String[]{"从相册选择", "拍照上传"}, -1,
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Intent intent = null;
                        switch (which) {
                            case 0://相册
                            //调用打开相册的方法
                                break;
                            case 1:// 拍照
                               //调用打开拍照的方法,注意要提前获取相机的权限
                                break;
                            default:
                                break;
                        }
                        dialog.dismiss();
                    }
                })
        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
            }
        });
choiceBuilder.create();
choiceBuilder.show();

打开相册

Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
startActivityForResult(intent, 2);

打开拍照

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent,1);

获得返回得信息,

重写onActivityResult方法获得传递得另外一个Activity传递的内容

获得相册的选中的图片URL

if(resultCode==-1)//判断是否选中OK
Uri uri = data.getData();

将这个url转成路径即可

android版本>19以上的采用

public String URLToString(){
String imagePath = null;
if (DocumentsContract.isDocumentUri(this, uri)) {
    // 如果是document类型的Uri,则通过document id处理
    String docId = DocumentsContract.getDocumentId(uri);
    if ("com.android.providers.media.documents".equals(uri.getAuthority())) {
        String id = docId.split(": ")[1]; // 解析出数字格式的id
        String selection = MediaStore.Images.Media._ID + "=" + id;
        imagePath = getImagePath(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(contentUri, null);
    }
} else if ("content".equalsIgnoreCase(uri.getScheme())) {
    // 如果是content类型的Uri,则使用普通方式处理
    imagePath = getImagePath(uri, null);
} else if ("file".equalsIgnoreCase(uri.getScheme())) {
    // 如果是file类型的Uri,直接获取图片路径即可
    imagePath = uri.getPath();
}
return imagePath;
}

android版本<19以上的采用
getImagePath(uri, null);

@SuppressLint("Range")
public String getImagePath(Uri uri, String selection) {
    String path = null;
    // 通过Uri和selection来获取真实的图片路径
    Cursor cursor = getContentResolver().query(uri, null, selection, null, null);
    if (cursor != null) {
        if (cursor.moveToFirst()) {
            path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
        }
        cursor.close();
    }
    return path;
}

获取拍照的内容

Bitmap bit = (Bitmap)data.getExtras().get("data");
File f = new File(getExternalCacheDir(),"head.png");
if (f.exists()) {
    f.delete();
}
try {
    FileOutputStream outputStream = new FileOutputStream(f);
    bit.compress(CompressFormat.PNG, 100, outputStream);
    outputStream.flush();
    outputStream.close();
//向Unity传递路径
    this.SendUnityPath(f.getPath());
} catch (FileNotFoundException var8) {
    var8.printStackTrace();
} catch (IOException var9) {
    var9.printStackTrace();
}

最后得到这个路径在调用Unity中的接收方法
通过文件流读取的方式获取字节流,再通过字节流转成Texture2D,或者根据需求转成Sprite可以了

猜你喜欢

转载自blog.csdn.net/weixin_44806700/article/details/122666203