我已经从谷歌阅读了这个标准描述: https://developer.android.com/training/sharing/receive
在此之后,我更新了我的 AndroidManifest.xml 以包含一个 intent-filter 以接受来自其他应用程序的共享(对于各种 mime 类型)。
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".SplashActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="SnapLinx Share"
android:theme="@style/SplashTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<action android:name="android.intent.action.VIEW" />
<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/*" />
<data android:mimeType="text/*" />
<data android:mimeType="video/*" />
<data android:mimeType="audeo/*" />
<data android:mimeType="application/*" />
</intent-filter>
</activity>
<activity
android:name=".SignInActivity"
android:label="@string/title_activity_sign_in"
android:theme="@style/AppTheme.NoActionBar">
</activity>
<activity
android:name=".SignUpActivity"
android:label="SignUpActivity"
android:theme="@style/AppTheme.NoActionBar">
</activity>
<activity
android:name=".ForgotPasswordActivity"
android:label="ForgotPasswordActivity"
android:theme="@style/AppTheme.NoActionBar">
</activity>
<activity
android:name=".MainActivity"
android:label="SnapLinx Share"
android:theme="@style/AppTheme">
</activity>
<service
android:name=".BLEScanIntentSvc"
android:enabled="true"
android:exported="false">
</service>
<service
android:name=".NotificationIntentSvc"
android:exported="false">
</service>
</application>
我可以将我的应用程序视为其他应用程序中的共享选项,当我在其他应用程序中选择一个文件(例如照片应用程序中的图像文件)并选择与我的应用程序共享的选项时,我的应用程序将启动正好。首先,我试图在我的 MainActivity onCreate() 中简单地记录共享参数。看这段代码:
Intent intent = getIntent();
String action = intent.getAction();
String type = intent.getType();
/ First try at getting Uri obj.
Uri FileURI = intent.getData();
if (Intent.ACTION_SEND.equals(action) && type != null)
{
Log.i(LOGTAG, "File intent. Type: " + type);
Log.i(LOGTAG, "File intent. Action: " + action);
FileURI = (Uri) intent.getParcelableExtra(Intent.EXTRA_TEXT);
if (FileURI != null)
{
Log.i(LOGTAG, "File intent. URL: " + FileURI.toString());
}
}
检索到的操作和类型字符串(“android.intent.action.SEND”和“image/*”)是正确的,但我无法获取我从照片应用共享的文件的 Uri。 intent.getData() 和 intent.getParcelableExtra(Intent.EXTRA_TEXT) 和 intent.getParcelableExtra(Intent.EXTRA_STREAM) 总是返回 null。
如果我中断调试器,所有 Intent 属性(期望支持 Uri 访问)看起来都是空的(即 mCatagories=null、mClipData=null mData=null、mExtras=null)。
标志由 mFlags=524288 指示....不确定是否有帮助。
我在 28 时使用编译 SDK 和目标 SDK 进行构建,在 23 时使用最小 SDK。
我的测试设备是运行 8.0.0 的 Galaxy S7。
如有任何建议,我们将不胜感激。
马克·J
最佳答案
尝试使用 Intent.EXTRA_STREAM 而不是 Intent.EXTRA_TEXT。 像这样 (Uri)intent.getParcelableExtra(Intent.EXTRA_STREAM);
关于android - 如何从 Android App Incoming Intent 获取 URI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54470314/