登录
注册
开源
企业版
高校版
搜索
帮助中心
使用条款
关于我们
开源
企业版
高校版
私有云
Gitee AI
NEW
我知道了
查看详情
登录
注册
代码拉取完成,页面将自动刷新
开源项目
>
手机/移动开发
>
Android组件/项目
&&
捐赠
捐赠前请先登录
取消
前往登录
扫描微信二维码支付
取消
支付完成
支付提示
将跳转至支付宝完成支付
确定
取消
Watch
不关注
关注所有动态
仅关注版本发行动态
关注但不提醒动态
5
Star
43
Fork
19
黎明白昼
/
HQW
代码
Issues
17
Pull Requests
0
Wiki
统计
流水线
服务
Gitee Pages
质量分析
Jenkins for Gitee
腾讯云托管
腾讯云 Serverless
悬镜安全
阿里云 SAE
Codeblitz
我知道了,不再自动展开
更新失败,请稍后重试!
Issues
/
详情
移除标识
内容风险标识
本任务被
标识为内容中包含有代码安全 Bug 、隐私泄露等敏感信息,仓库外成员不可访问
IM的文件选择调用
待办的
#I93MUP
黎明白昼
拥有者
创建于
2024-02-26 09:28
``` package com.tencent.qcloud.tuikit.timcommon.util; import android.app.Activity; import android.content.ClipData; import android.content.Context; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.provider.MediaStore; import android.text.TextUtils; import android.util.Pair; import androidx.activity.result.ActivityResult; import androidx.activity.result.ActivityResultCallback; import androidx.activity.result.ActivityResultCaller; import androidx.activity.result.ActivityResultLauncher; import androidx.activity.result.contract.ActivityResultContract; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.fragment.app.FragmentActivity; import com.tencent.qcloud.tuicore.TUICore; import com.tencent.qcloud.tuicore.interfaces.TUIValueCallback; import java.util.ArrayList; import java.util.Collections; import java.util.LinkedHashSet; import java.util.List; public class ActivityResultResolver { public static final String CONTENT_TYPE_ALL = "*/*"; public static final String CONTENT_TYPE_IMAGE = "image/*"; public static final String CONTENT_TYPE_VIDEO = "video/*"; private static final String CONTENT_KEY_TYPE = "ContentType"; private static final String URI = "Uri"; private static final String METHOD = "Method"; private static final String KEY_DATA = "Data"; private static final String METHOD_GET_SINGLE_CONTENT = "MethodGetSingleContent"; private static final String METHOD_GET_MULTIPLE_CONTENT = "MethodGetMultipleContent"; private static final String METHOD_TAKE_PICTURE = "MethodTakePicture"; private static final String METHOD_TAKE_VIDEO = "MethodTakeVideo"; private ActivityResultResolver() {} public static void getSingleContent(ActivityResultCaller activityResultCaller, @NonNull String type, TUIValueCallback<Uri> callback) { getContent(activityResultCaller, new String[] {type}, false, new TUIValueCallback<List<Uri>>() { @Override public void onSuccess(List<Uri> list) { if (list != null && !list.isEmpty()) { TUIValueCallback.onSuccess(callback, list.get(0)); } else { TUIValueCallback.onError(callback, -1, "getSingleContent result list is empty"); } } @Override public void onError(int errorCode, String errorMessage) { TUIValueCallback.onError(callback, errorCode, errorMessage); } }); } public static void getSingleContent(ActivityResultCaller activityResultCaller, @NonNull String[] type, TUIValueCallback<Uri> callback) { getContent(activityResultCaller, type, false, new TUIValueCallback<List<Uri>>() { @Override public void onSuccess(List<Uri> list) { if (list != null && !list.isEmpty()) { TUIValueCallback.onSuccess(callback, list.get(0)); } else { TUIValueCallback.onError(callback, -1, "getSingleContent result list is empty"); } } @Override public void onError(int errorCode, String errorMessage) { TUIValueCallback.onError(callback, errorCode, errorMessage); } }); } public static void getMultipleContent(ActivityResultCaller activityResultCaller, @NonNull String type, TUIValueCallback<List<Uri>> callback) { getContent(activityResultCaller, new String[] {type}, true, callback); } public static void getMultipleContent(ActivityResultCaller activityResultCaller, @NonNull String[] type, TUIValueCallback<List<Uri>> callback) { getContent(activityResultCaller, type, true, callback); } private static void getContent( ActivityResultCaller activityResultCaller, @NonNull String[] types, boolean isMultiContent, TUIValueCallback<List<Uri>> callback) { Bundle bundle = new Bundle(); bundle.putStringArray(CONTENT_KEY_TYPE, types); bundle.putString(METHOD, isMultiContent ? METHOD_GET_MULTIPLE_CONTENT : METHOD_GET_SINGLE_CONTENT); TUICore.startActivityForResult(activityResultCaller, ActivityResultProxyActivity.class, bundle, new ActivityResultCallback<ActivityResult>() { @Override public void onActivityResult(ActivityResult result) { if (result.getData() != null) { TUIValueCallback.onSuccess(callback, (List<Uri>) result.getData().getSerializableExtra(KEY_DATA)); } } }); } public static void takePicture(ActivityResultCaller activityResultCaller, @NonNull Uri uri, TUIValueCallback<Boolean> callback) { takePictureVideo(activityResultCaller, uri, true, callback); } public static void takeVideo(ActivityResultCaller activityResultCaller, @NonNull Uri uri, TUIValueCallback<Boolean> callback) { takePictureVideo(activityResultCaller, uri, false, callback); } private static void takePictureVideo(ActivityResultCaller activityResultCaller, @NonNull Uri uri, boolean isPicture, TUIValueCallback<Boolean> callback) { Bundle bundle = new Bundle(); if (isPicture) { bundle.putString(METHOD, METHOD_TAKE_PICTURE); } else { bundle.putString(METHOD, METHOD_TAKE_VIDEO); } bundle.putParcelable(URI, uri); TUICore.startActivityForResult(activityResultCaller, ActivityResultProxyActivity.class, bundle, new ActivityResultCallback<ActivityResult>() { @Override public void onActivityResult(ActivityResult result) { if (result.getData() != null) { TUIValueCallback.onSuccess(callback, result.getData().getBooleanExtra(KEY_DATA, false)); } } }); } private static class GetContentsContract extends ActivityResultContract<Pair<String[], Boolean>, List<Uri>> { @NonNull @Override public Intent createIntent(@NonNull Context context, @NonNull Pair<String[], Boolean> input) { Intent intent = new Intent(Intent.ACTION_GET_CONTENT); intent.addCategory(Intent.CATEGORY_OPENABLE); String[] type = input.first; boolean allowMultiple = input.second; if (type.length == 1) { intent.setType(type[0]); } else if (type.length > 1) { intent.setType(type[0]); intent.putExtra(Intent.EXTRA_MIME_TYPES, type); } intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, allowMultiple); return intent; } @NonNull @Override public final List<Uri> parseResult(int resultCode, @Nullable Intent intent) { if (intent == null || resultCode != Activity.RESULT_OK) { return Collections.emptyList(); } else { return getClipDataUris(intent); } } @NonNull static List<Uri> getClipDataUris(@NonNull Intent intent) { // Use a LinkedHashSet to maintain any ordering that may be // present in the ClipData LinkedHashSet<Uri> resultSet = new LinkedHashSet<>(); if (intent.getData() != null) { resultSet.add(intent.getData()); } ClipData clipData = intent.getClipData(); if (clipData == null && resultSet.isEmpty()) { return Collections.emptyList(); } else if (clipData != null) { for (int i = 0; i < clipData.getItemCount(); i++) { Uri uri = clipData.getItemAt(i).getUri(); if (uri != null) { resultSet.add(uri); } } } return new ArrayList<>(resultSet); } } private static class TakePictureVideoContract extends ActivityResultContract<Pair<Uri, Boolean>, Boolean> { private boolean isTakePicture; @NonNull @Override public Intent createIntent(@NonNull Context context, @NonNull Pair<Uri, Boolean> input) { isTakePicture = input.second; if (isTakePicture) { return new Intent(MediaStore.ACTION_IMAGE_CAPTURE) .putExtra(MediaStore.EXTRA_OUTPUT, input.first) .addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION) .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); } else { return new Intent(MediaStore.ACTION_VIDEO_CAPTURE) .putExtra(MediaStore.EXTRA_OUTPUT, input.first) .addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION) .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); } } @NonNull @Override public final Boolean parseResult(int resultCode, @Nullable Intent intent) { if (isTakePicture) { return resultCode == Activity.RESULT_OK; } else { return intent != null && resultCode == Activity.RESULT_OK; } } } public static class ActivityResultProxyActivity extends FragmentActivity { @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); Intent intent = getIntent(); String method = intent.getStringExtra(METHOD); if (TextUtils.equals(method, METHOD_GET_SINGLE_CONTENT)) { getSingleContent(intent); } else if (TextUtils.equals(method, METHOD_GET_MULTIPLE_CONTENT)) { getMultipleContent(intent); } else if (TextUtils.equals(method, METHOD_TAKE_PICTURE)) { takePicture(intent); } else if (TextUtils.equals(method, METHOD_TAKE_VIDEO)) { takeVideo(intent); } } private void getSingleContent(Intent intent) { String[] types = intent.getStringArrayExtra(CONTENT_KEY_TYPE); getContent(types, false); } private void getMultipleContent(Intent intent) { String[] types = intent.getStringArrayExtra(CONTENT_KEY_TYPE); getContent(types, true); } private void getContent(String[] types, boolean isMultiple) { ActivityResultLauncher<Pair<String[], Boolean>> launcher = this.registerForActivityResult(new GetContentsContract(), new ActivityResultCallback<List<Uri>>() { @Override public void onActivityResult(List<Uri> result) { Intent dataIntent = new Intent(); dataIntent.putExtra(KEY_DATA, new ArrayList<>(result)); setResult(Activity.RESULT_OK, dataIntent); finish(); } }); try { launcher.launch(Pair.create(types, isMultiple)); } catch (Exception e) { e.printStackTrace(); } } private void takePicture(Intent intent) { takePictureVideo(intent, true); } private void takeVideo(Intent intent) { takePictureVideo(intent, false); } private void takePictureVideo(Intent intent, boolean isPicture) { Uri uri = intent.getParcelableExtra(URI); ActivityResultLauncher<Pair<Uri, Boolean>> launcher = this.registerForActivityResult(new TakePictureVideoContract(), new ActivityResultCallback<Boolean>() { @Override public void onActivityResult(Boolean result) { Intent dataIntent = new Intent(); dataIntent.putExtra(KEY_DATA, result); setResult(Activity.RESULT_OK, dataIntent); finish(); } }); try { launcher.launch(Pair.create(uri, isPicture)); } catch (Exception e) { e.printStackTrace(); } } } } ```
``` package com.tencent.qcloud.tuikit.timcommon.util; import android.app.Activity; import android.content.ClipData; import android.content.Context; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.provider.MediaStore; import android.text.TextUtils; import android.util.Pair; import androidx.activity.result.ActivityResult; import androidx.activity.result.ActivityResultCallback; import androidx.activity.result.ActivityResultCaller; import androidx.activity.result.ActivityResultLauncher; import androidx.activity.result.contract.ActivityResultContract; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.fragment.app.FragmentActivity; import com.tencent.qcloud.tuicore.TUICore; import com.tencent.qcloud.tuicore.interfaces.TUIValueCallback; import java.util.ArrayList; import java.util.Collections; import java.util.LinkedHashSet; import java.util.List; public class ActivityResultResolver { public static final String CONTENT_TYPE_ALL = "*/*"; public static final String CONTENT_TYPE_IMAGE = "image/*"; public static final String CONTENT_TYPE_VIDEO = "video/*"; private static final String CONTENT_KEY_TYPE = "ContentType"; private static final String URI = "Uri"; private static final String METHOD = "Method"; private static final String KEY_DATA = "Data"; private static final String METHOD_GET_SINGLE_CONTENT = "MethodGetSingleContent"; private static final String METHOD_GET_MULTIPLE_CONTENT = "MethodGetMultipleContent"; private static final String METHOD_TAKE_PICTURE = "MethodTakePicture"; private static final String METHOD_TAKE_VIDEO = "MethodTakeVideo"; private ActivityResultResolver() {} public static void getSingleContent(ActivityResultCaller activityResultCaller, @NonNull String type, TUIValueCallback<Uri> callback) { getContent(activityResultCaller, new String[] {type}, false, new TUIValueCallback<List<Uri>>() { @Override public void onSuccess(List<Uri> list) { if (list != null && !list.isEmpty()) { TUIValueCallback.onSuccess(callback, list.get(0)); } else { TUIValueCallback.onError(callback, -1, "getSingleContent result list is empty"); } } @Override public void onError(int errorCode, String errorMessage) { TUIValueCallback.onError(callback, errorCode, errorMessage); } }); } public static void getSingleContent(ActivityResultCaller activityResultCaller, @NonNull String[] type, TUIValueCallback<Uri> callback) { getContent(activityResultCaller, type, false, new TUIValueCallback<List<Uri>>() { @Override public void onSuccess(List<Uri> list) { if (list != null && !list.isEmpty()) { TUIValueCallback.onSuccess(callback, list.get(0)); } else { TUIValueCallback.onError(callback, -1, "getSingleContent result list is empty"); } } @Override public void onError(int errorCode, String errorMessage) { TUIValueCallback.onError(callback, errorCode, errorMessage); } }); } public static void getMultipleContent(ActivityResultCaller activityResultCaller, @NonNull String type, TUIValueCallback<List<Uri>> callback) { getContent(activityResultCaller, new String[] {type}, true, callback); } public static void getMultipleContent(ActivityResultCaller activityResultCaller, @NonNull String[] type, TUIValueCallback<List<Uri>> callback) { getContent(activityResultCaller, type, true, callback); } private static void getContent( ActivityResultCaller activityResultCaller, @NonNull String[] types, boolean isMultiContent, TUIValueCallback<List<Uri>> callback) { Bundle bundle = new Bundle(); bundle.putStringArray(CONTENT_KEY_TYPE, types); bundle.putString(METHOD, isMultiContent ? METHOD_GET_MULTIPLE_CONTENT : METHOD_GET_SINGLE_CONTENT); TUICore.startActivityForResult(activityResultCaller, ActivityResultProxyActivity.class, bundle, new ActivityResultCallback<ActivityResult>() { @Override public void onActivityResult(ActivityResult result) { if (result.getData() != null) { TUIValueCallback.onSuccess(callback, (List<Uri>) result.getData().getSerializableExtra(KEY_DATA)); } } }); } public static void takePicture(ActivityResultCaller activityResultCaller, @NonNull Uri uri, TUIValueCallback<Boolean> callback) { takePictureVideo(activityResultCaller, uri, true, callback); } public static void takeVideo(ActivityResultCaller activityResultCaller, @NonNull Uri uri, TUIValueCallback<Boolean> callback) { takePictureVideo(activityResultCaller, uri, false, callback); } private static void takePictureVideo(ActivityResultCaller activityResultCaller, @NonNull Uri uri, boolean isPicture, TUIValueCallback<Boolean> callback) { Bundle bundle = new Bundle(); if (isPicture) { bundle.putString(METHOD, METHOD_TAKE_PICTURE); } else { bundle.putString(METHOD, METHOD_TAKE_VIDEO); } bundle.putParcelable(URI, uri); TUICore.startActivityForResult(activityResultCaller, ActivityResultProxyActivity.class, bundle, new ActivityResultCallback<ActivityResult>() { @Override public void onActivityResult(ActivityResult result) { if (result.getData() != null) { TUIValueCallback.onSuccess(callback, result.getData().getBooleanExtra(KEY_DATA, false)); } } }); } private static class GetContentsContract extends ActivityResultContract<Pair<String[], Boolean>, List<Uri>> { @NonNull @Override public Intent createIntent(@NonNull Context context, @NonNull Pair<String[], Boolean> input) { Intent intent = new Intent(Intent.ACTION_GET_CONTENT); intent.addCategory(Intent.CATEGORY_OPENABLE); String[] type = input.first; boolean allowMultiple = input.second; if (type.length == 1) { intent.setType(type[0]); } else if (type.length > 1) { intent.setType(type[0]); intent.putExtra(Intent.EXTRA_MIME_TYPES, type); } intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, allowMultiple); return intent; } @NonNull @Override public final List<Uri> parseResult(int resultCode, @Nullable Intent intent) { if (intent == null || resultCode != Activity.RESULT_OK) { return Collections.emptyList(); } else { return getClipDataUris(intent); } } @NonNull static List<Uri> getClipDataUris(@NonNull Intent intent) { // Use a LinkedHashSet to maintain any ordering that may be // present in the ClipData LinkedHashSet<Uri> resultSet = new LinkedHashSet<>(); if (intent.getData() != null) { resultSet.add(intent.getData()); } ClipData clipData = intent.getClipData(); if (clipData == null && resultSet.isEmpty()) { return Collections.emptyList(); } else if (clipData != null) { for (int i = 0; i < clipData.getItemCount(); i++) { Uri uri = clipData.getItemAt(i).getUri(); if (uri != null) { resultSet.add(uri); } } } return new ArrayList<>(resultSet); } } private static class TakePictureVideoContract extends ActivityResultContract<Pair<Uri, Boolean>, Boolean> { private boolean isTakePicture; @NonNull @Override public Intent createIntent(@NonNull Context context, @NonNull Pair<Uri, Boolean> input) { isTakePicture = input.second; if (isTakePicture) { return new Intent(MediaStore.ACTION_IMAGE_CAPTURE) .putExtra(MediaStore.EXTRA_OUTPUT, input.first) .addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION) .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); } else { return new Intent(MediaStore.ACTION_VIDEO_CAPTURE) .putExtra(MediaStore.EXTRA_OUTPUT, input.first) .addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION) .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); } } @NonNull @Override public final Boolean parseResult(int resultCode, @Nullable Intent intent) { if (isTakePicture) { return resultCode == Activity.RESULT_OK; } else { return intent != null && resultCode == Activity.RESULT_OK; } } } public static class ActivityResultProxyActivity extends FragmentActivity { @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); Intent intent = getIntent(); String method = intent.getStringExtra(METHOD); if (TextUtils.equals(method, METHOD_GET_SINGLE_CONTENT)) { getSingleContent(intent); } else if (TextUtils.equals(method, METHOD_GET_MULTIPLE_CONTENT)) { getMultipleContent(intent); } else if (TextUtils.equals(method, METHOD_TAKE_PICTURE)) { takePicture(intent); } else if (TextUtils.equals(method, METHOD_TAKE_VIDEO)) { takeVideo(intent); } } private void getSingleContent(Intent intent) { String[] types = intent.getStringArrayExtra(CONTENT_KEY_TYPE); getContent(types, false); } private void getMultipleContent(Intent intent) { String[] types = intent.getStringArrayExtra(CONTENT_KEY_TYPE); getContent(types, true); } private void getContent(String[] types, boolean isMultiple) { ActivityResultLauncher<Pair<String[], Boolean>> launcher = this.registerForActivityResult(new GetContentsContract(), new ActivityResultCallback<List<Uri>>() { @Override public void onActivityResult(List<Uri> result) { Intent dataIntent = new Intent(); dataIntent.putExtra(KEY_DATA, new ArrayList<>(result)); setResult(Activity.RESULT_OK, dataIntent); finish(); } }); try { launcher.launch(Pair.create(types, isMultiple)); } catch (Exception e) { e.printStackTrace(); } } private void takePicture(Intent intent) { takePictureVideo(intent, true); } private void takeVideo(Intent intent) { takePictureVideo(intent, false); } private void takePictureVideo(Intent intent, boolean isPicture) { Uri uri = intent.getParcelableExtra(URI); ActivityResultLauncher<Pair<Uri, Boolean>> launcher = this.registerForActivityResult(new TakePictureVideoContract(), new ActivityResultCallback<Boolean>() { @Override public void onActivityResult(Boolean result) { Intent dataIntent = new Intent(); dataIntent.putExtra(KEY_DATA, result); setResult(Activity.RESULT_OK, dataIntent); finish(); } }); try { launcher.launch(Pair.create(uri, isPicture)); } catch (Exception e) { e.printStackTrace(); } } } } ```
评论 (
0
)
黎明白昼
创建了
任务
登录
后才可以发表评论
状态
待办的
待办的
进行中
已完成
已关闭
负责人
未设置
标签
未设置
标签管理
里程碑
未关联里程碑
未关联里程碑
Pull Requests
未关联
未关联
关联的 Pull Requests 被合并后可能会关闭此 issue
分支
未关联
分支 (2)
标签 (38)
master
demo
1.9.9
1.9.8
1.9.7
1.9.6
1.9.5
1.9.0
1.6.8
1.6.4
1.6.3
1.6.2
1.6.1
1.6.0
1.5.9
1.5.8
1.5.7
1.5.1
1.4.1
1.4.0
Beta1.3.7
1.3.6
1.3.5
1.3.4
1.3.3
1.2.8
1.2.7
1.2.6
1.2.5
1.2.4
1.2.3
1.2.2
1.2.1
1.1.7
1.1.6
1.1.5
1.1.2
1.1.1
1.0.9
1.0.8
开始日期   -   截止日期
-
置顶选项
不置顶
置顶等级:高
置顶等级:中
置顶等级:低
优先级
不指定
严重
主要
次要
不重要
参与者(1)
Android
1
https://gitee.com/BAILIS/HQW.git
[email protected]
:BAILIS/HQW.git
BAILIS
HQW
HQW
点此查找更多帮助
搜索帮助
Git 命令在线学习
如何在 Gitee 导入 GitHub 仓库
Git 仓库基础操作
企业版和社区版功能对比
SSH 公钥设置
如何处理代码冲突
仓库体积过大,如何减小?
如何找回被删除的仓库数据
Gitee 产品配额说明
GitHub仓库快速导入Gitee及同步更新
什么是 Release(发行版)
将 PHP 项目自动发布到 packagist.org
评论
仓库举报
回到顶部
登录提示
该操作需登录 Gitee 帐号,请先登录后再操作。
立即登录
没有帐号,去注册