【问题标题】:How to use intent for sharing gallery picture to application如何使用意图将图库图片共享到应用程序
【发布时间】:2014-05-12 08:37:35
【问题描述】:

我已将我的应用程序添加到图库顶部的共享图标中,但到目前为止它只打开了我的应用程序,但是如何将图片 URI 和内容发送到我的应用程序?我知道我必须使用意图?周围有什么指南可以看吗?谷歌网站上的 easy-share-action 指南在这里对我没有多大帮助。

【问题讨论】:

  • 它有点令人困惑的问题。你能具体分享一下你想做什么吗?

标签: android android-intent


【解决方案1】:

试试这个方法:

<activity
    android:name="yourActivity"
    android:icon="@drawable/ic_launcher"
    android:label="test">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>

   <intent-filter>
        <action android:name="android.intent.action.SEND" />

        <category android:name="android.intent.category.DEFAULT" />

        <data android:mimeType="image/*" />
    </intent-filter>

    <intent-filter>
        <action android:name="android.intent.action.SEND" />

        <category android:name="android.intent.category.DEFAULT" />

        <data android:mimeType="text/plain" />
    </intent-filter>

    <intent-filter>
        <action android:name="android.intent.action.SEND_MULTIPLE" />

        <category android:name="android.intent.category.DEFAULT" />

        <data android:mimeType="image/*" />
    </intent-filter>
</activity>

处理传入的内容

void onCreate (Bundle savedInstanceState) {
...
// Get intent, action and MIME type
Intent intent = getIntent();
String action = intent.getAction();
String type = intent.getType();

if (Intent.ACTION_SEND.equals(action) && type != null) {
    if ("text/plain".equals(type)) {
        handleSendText(intent); // Handle text being sent
    } else if (type.startsWith("image/")) {
        handleSendImage(intent); // Handle single image being sent
    }
} else if (Intent.ACTION_SEND_MULTIPLE.equals(action) && type != null) {
    if (type.startsWith("image/")) {
        handleSendMultipleImages(intent); // Handle multiple images being sent
    }
} else {
    // Handle other intents, such as being started from the home screen
}
...
}

void handleSendText(Intent intent) {
String sharedText = intent.getStringExtra(Intent.EXTRA_TEXT);
if (sharedText != null) {
    // Update UI to reflect text being shared
}
}

void handleSendImage(Intent intent) {
Uri imageUri = (Uri) intent.getParcelableExtra(Intent.EXTRA_STREAM);
if (imageUri != null) {
    // Update UI to reflect image being shared
}
}

 void handleSendMultipleImages(Intent intent) {
ArrayList<Uri> imageUris = intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM);
if (imageUris != null) {
    // Update UI to reflect multiple images being shared
 }
}

欲了解更多信息,请访问Receiving Simple Data from Other AppsSending Simple Data to Other Apps

【讨论】:

    【解决方案2】:

    要在 Activity 中处理图像,您需要检查 Intent 是否具有图像。

    Intent intent = getIntent();
    Bundle extras = intent.getExtras();
    String action = intent.getAction();
    
    if (Intent.ACTION_SEND.equals(action)) {   
        if (extras.containsKey(Intent.EXTRA_STREAM)) {
            // Get resource path of the stream, which in this case will be the image
        }
    }
    

    此外,您需要在清单中添加要过滤这些共享图像意图的内容。

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

    【讨论】:

      猜你喜欢
      • 2017-01-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-29
      • 1970-01-01
      • 1970-01-01
      • 2020-05-11
      相关资源
      最近更新 更多