如何在Android中更改库中的共享照片选项?

时间:2017-11-29 12:18:51

标签: android android-intent share

我有分享意图。用户可以从其他应用共享图像,视频和文本。我不希望用户从用户的照片库中选择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);
    }
}

2 个答案:

答案 0 :(得分:0)

首先,您需要阅读this了解如何从其他应用接收数据。你需要分开处理:

ipython

适用于所有不同类型的数据。

答案 1 :(得分:0)

我解决了从ShareActivity的onCreate方法控制它。

public class ShareActivity extends Activity {

   @Override
    protected void onCreate(Bundle savedInstanceState, int count) {

     if (!sharedMessageControl()) {
         finishAffinity();
         }
         setContentView(R.layout.share_activity);
         }
     }

        private boolean sharedMessageControl() {

            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.Long);

                                return false;
                            } else {
                                return true;
                            }
                        }//image

                    }
                }

                return true;

            } catch (Exception e) {
                Mylog.printStackTrace(TAG + " sharedMessageControl error ", e);
                return false;
            }
        }