【问题标题】:How can I changing shared photo options from gallery in android?如何从 android 的图库中更改共享照片选项?
【发布时间】: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


【解决方案1】:

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

<intent-filter>
    <action android:name="android.intent.action.SEND" />
    <category android:name="android.intent.category.DEFAULT" />
    <data android:mimeType="image/*" />
</intent-filter>

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

【讨论】:

  • 谢谢,我知道这些,我可以得到意图,但我的问题不同
  • 有没有办法在照片库屏幕上控制这个?
  • 如果您可以先打开应用启动画面,然后在图库屏幕之后打开,则可以检查数据类型。因为在活动中总是可能的。
  • 其实我不想打开应用
  • 我解决了这个问题并写在下面。但是,如果您有其他解决方案,请您写信吗?
【解决方案2】:

我通过 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;
            }
        }

【讨论】:

    猜你喜欢
    • 2013-10-13
    • 2020-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-17
    • 2012-11-01
    • 2013-03-28
    • 1970-01-01
    相关资源
    最近更新 更多