tencent cloud

Tencent Cloud Super App as a Service

Customizing Image Selection

Download
Focus Mode
Font Size
Last updated: 2025-07-04 17:33:28
The mini program SDK provides a default image selection feature, which uses the Android system's built-in media picker when the mini program calls the multimedia selection (wx.chooseImage, wx.chooseVideo, wx.chooseMedia).
Custom image selection can be achieved by overriding the chooseMediaFromAlbum and chooseMediaFromCamera methods in the BaseMiniAppProxyImpl class.
API description:
Parameter context: The Activity instance of the current mini program page.
Parameter options: Media selection options. ChooseMediaOptions
Parameter listener: Image selection callback API. ChooseMediaProxy.IChooseMediaListener
Return value boolean: A return value of false indicates that the default image selection implementation is used, while a return value of true indicates that the custom image selection implementation is used.
/**
* Selects media files from the album
* @param context
* @param options
* @param listener
* @return
*/
public boolean chooseMediaFromAlbum(Activity context, ChooseMediaOptions options, ChooseMediaProxy.IChooseMediaListener listener)
/**
* Captures media files from the camera
* @param context
* @param options
* @param listener
* @return
*/
public boolean chooseMediaFromCamera(Activity context, ChooseMediaOptions options, ChooseMediaProxy.IChooseMediaListener listener)
Example:
public static final int REQUEST_CHOOSE_MEDIA = 110;
public static final int REQUEST_CHOOSE_MEDIA_CAMERA = 111;

@Override
public void chooseMediaFromAlbum(Activity context, ChooseMediaOptions options, IChooseMediaListener listener) {
try {
TmfMiniSDK.addActivityResultListener(new IActivityResultListener() {
@Override
public boolean doOnActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CHOOSE_MEDIA && resultCode == Activity.RESULT_OK) {
ArrayList<Uri> picturePath = new ArrayList<>();
if (data == null) {
Log.e(TAG, "empty intent ,failed getting from chooseImage");
listener.onResult(false, null);
return false;
}
if (null != data.getClipData()) {
Log.d(TAG, "use clipdata as intent uri ");
ClipData clipData = data.getClipData();
for (int i = 0; i < clipData.getItemCount(); i++) {
picturePath.add(clipData.getItemAt(i).getUri());
}
Log.d(TAG, "send to mini with path " + picturePath.size());
} else if (null != data.getData()) {
Log.d(TAG, "select picture path is " + data.getData());
picturePath.add(data.getData());
}
listener.onResult(true, picturePath);
} else {
listener.onResult(false, null);
}
MiniSDK.removeActivityResultListener(this);
return true;
}
});
Intent chooserIntent = new Intent(Intent.ACTION_GET_CONTENT);
if (options.maxCount > 1) {
chooserIntent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
}
switch (options.mediaType) {
case ChooseMediaOptions.MEDIA_TYPE_IMAGE:
chooserIntent.setType("image/*");
break;
case ChooseMediaOptions.MEDIA_TYPE_VIDEO:
chooserIntent.setType("video/*");
break;
case ChooseMediaOptions.MEDIA_TYPE_MIX:
chooserIntent.setType("*/*");
chooserIntent.putExtra(Intent.EXTRA_MIME_TYPES, new String[]{"image/*", "video/*"});
default:
chooserIntent.setType("image/*");
break;
}
context.startActivityForResult(chooserIntent, REQUEST_CHOOSE_MEDIA);
} catch (Exception e) {
listener.onResult(false, null);
}
}

@Override
public void chooseMediaFromCamera(Activity context, ChooseMediaOptions options, IChooseMediaListener listener) {
Intent takePhotoIntent;
if (options.mediaType == ChooseMediaOptions.MEDIA_TYPE_VIDEO) {
takePhotoIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
int duration = options.duration;
if (duration < 1 || duration > 60000) {
duration = 60000;
}
takePhotoIntent.putExtra(MediaStore.EXTRA_DURATION_LIMIT, duration);
if (options.camera == ChooseMediaOptions.CAMERA_TYPE_FRONT) {
takePhotoIntent.putExtra("android.intent.extras.CAMERA_FACING", 1);
}
} else {
takePhotoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
}

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
takePhotoIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
}

if (takePhotoIntent.resolveActivity(context.getPackageManager()) == null) {
Log.e(TAG, "resolveActivity failed ,has no camera app");
listener.onResult(false, null);
return;
}

if (options.cameraOutput == null) {
Log.e(TAG, "createImageFile failed ");
listener.onResult(false, null);
return;
}

Uri imageUri = options.cameraOutput;
takePhotoIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
context.startActivityForResult(takePhotoIntent, REQUEST_CHOOSE_MEDIA_CAMERA);
TmfMiniSDK.addActivityResultListener(new IActivityResultListener() {
@Override
public boolean doOnActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode != REQUEST_CHOOSE_MEDIA_CAMERA) {
return false;
}
if (resultCode != Activity.RESULT_OK) {
listener.onResult(false, null);
} else {
ArrayList<Uri> picturePath = new ArrayList<>();
picturePath.add(imageUri);
listener.onResult(true, picturePath);
}
MiniSDK.removeActivityResultListener(this);
return true;
}
});
}





Help and Support

Was this page helpful?

Help us improve! Rate your documentation experience in 5 mins.

Feedback