如何使用意图共享图库图片到应用程序

时间:2014-05-12 08:37:36

标签: android android-intent

我已将我的应用程序添加到库中顶部的共享图标,但到目前为止它只打开我的应用程序,但如何将图片URI和内容发送到我的应用程序?我明白我必须使用意图?我可以看看周围有什么指南吗?谷歌网站上的轻松分享行动指南在这里对我没什么帮助。

2 个答案:

答案 0 :(得分:3)

尝试这种方式:

<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

答案 1 :(得分:0)

要处理活动中的图像,您需要检查意图是否包含图像。

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>