我的应用通过 TCP 连接发送文件(所有 mime 类型),因此我希望我的应用出现在 Android 共享菜单中。
我在 AndroidManifest.xml
中将以下 Intent 过滤器添加到我的 Activity
<intent-filter ><!--intent filter for single file sharing-->
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="*/*" />
</intent-filter>
<intent-filter ><!--intent filter for sharing multiple files-->
<action android:name="android.intent.action.SEND_MULTIPLE" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="*/*" />
</intent-filter>
现在我将它添加到 Activity
中,该 Activity 将在单击共享操作时启动
Intent intent = getIntent();
if (Intent.ACTION_SEND.equals(intent.getAction())) {
Uri uri = (Uri) intent.getParcelableExtra(Intent.EXTRA_STREAM);
if (uri != null) {
fileset = new HashSet();
fileset.add(getPathfromUri(uri));
}
} else if (Intent.ACTION_SEND_MULTIPLE.equals(intent.getAction())) {
ArrayList<Uri> uris = intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM);
if (uris != null) {
fileset = new HashSet();
for(Uri uri : uris) {
fileset.add(getPathfromUri(uri));
}
}
}
我使用这种方法来生成要共享的文件的绝对路径。
public String getPathfromUri(Uri uri) {
if(uri.toString().startsWith("file://")) {
return uri.getPath();
}
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(uri, projection, null, null, null);
startManagingCursor(cursor);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
String path= cursor.getString(column_index);
//cursor.close();
return path;
}
上述方法正确检索图像/视频和其他文件的绝对路径。是否有我遗漏了什么或者有更好的方法来做到这一点?
除了 Share
菜单,我的手机上还有一个 Send Via
菜单。
有没有办法让我的应用进入这个列表?
最佳答案
我已经在这里概述了如何进入菜单:
http://blog.blundellapps.com/add-your-app-to-the-android-menu/
基本上它有你的 mimetype / :
<intent-filter>
<action android:name="android.intent.action.GET_CONTENT" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="*/*" />
</intent-filter>
如果你这样做但它没有显示,这意味着他们正在使用一个 Intent 菜单,它是一个自定义共享列表,所以你不能强行进入。
关于android - 最佳实践 - 将您的应用添加到 Android 共享菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10788252/