【发布时间】:2017-11-29 12:18:50
【问题描述】:
我有分享意图。用户可以共享来自其他应用程序的图像、视频和文本。我不希望用户从用户的照片库中选择超过 10 张图片。
我已经在 ShareActivity 中编写了这个函数 (sharedMessagesControl),但我想检查一下另一个实用的解决方案。如何在活动屏幕打开之前检查这一点?有没有办法在照片库屏幕上控制它?我该怎么做?
清单:
<activity
android:name=".share.ShareActivity"
android:configChanges="touchscreen|keyboard|keyboardHidden|orientation|screenLayout|screenSize"
android:excludeFromRecents="true"
android:launchMode="singleTask"
android:screenOrientation="portrait"
android:windowSoftInputMode="stateHidden">
<intent-filter>
<action android:name="android.intent.action.SEND" />
<action android:name="android.intent.action.SEND_MULTIPLE"/>
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="image/*" />
<data android:mimeType="text/plain" />
<data android:mimeType="video/*" />
</intent-filter>
</activity>
方法如下:
ShareActivity(){
private void sharedMessagesControl() {
sharedIntent = getIntent();
try {
if ((sharedIntent != null) && (sharedIntent.getType() != null) &&
((sharedIntent.getPackage() == null) || (!sharedIntent.getPackage().equals(getPackageName())))) {
String receivedType = sharedIntent.getType();
String action = sharedIntent.getAction();
if (action != null && (action.equalsIgnoreCase(Intent.ACTION_SEND_MULTIPLE) || action.equalsIgnoreCase("android.intent.action.SEND_MULTIPLE"))) {
ArrayList<Uri> uris = sharedIntent.getParcelableArrayListExtra(Intent.EXTRA_STREAM);
if (uris != null && receivedType.contains("image/")) {
if (uris.size() > MAX_IMAGE_MEDIA_LIMIT) {
sharedIntent = null;
new MyToast(R.string.max_limit_image, MyToast.Length.Short);
}
}
}
}
} catch (Exception e) {
Mylog.printStackTrace(TAG + " shareMessageControl error ", e);
}
}
【问题讨论】:
-
通过intent检查是什么意思?
-
另一个实用的解决方案。请看我编辑的答案
-
我不明白你的问题意味着你想在画廊的意图实际打开之前检查画廊中是否有超过 10 张图片?
-
没错……
-
如果我为你提供算法呢?你能把那个算法转换成自己的代码吗?
标签: android android-intent share