我做一個Android應用程序。目標如下。Android的入門PDF文件的路徑,從我的應用程序
目的:
如果用戶希望在他們的Android移動設備隨時隨地共享PDF(或)圖像文件的形式,他們將我的應用程序在共享列表的選項。我想知道,用戶是否選擇了PDF文件或圖像。爲此我使用了下面的代碼。
在AndroidManifest.xml中
<activity
android:name="com.share.FileActivity"
android:screenOrientation="portrait"
android:theme="@android:style/Theme.NoTitleBar" >
<intent-filter>
<action android:name="android.intent.action.SEND" />
<data android:mimeType="application/pdf" />
<data android:mimeType="image/*" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
在FileActivity.java,以獲得所選擇的文件URI
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent myintent = getIntent();
Bundle extras = myintent.getExtras();
String action = myintent.getAction();
if (Intent.ACTION_SEND.equals(action)) {
if (extras.containsKey(Intent.EXTRA_STREAM)) {
URI uri = (Uri) extras.getParcelable(Intent.EXTRA_STREAM);
.
.
}
}
}
問題:
如果我打開的檔案總管我得到了正確的URI爲 「文件:///storage/emulated/0/Download/test.pdf」 PDF。以便我可以識別爲PDF或圖像。
但是如果我打開從谷歌驅動程序的PDF文件時,我收到以下URI「內容://com.google.android.apps.docs.storage.legacy/enc%3Du4Ss-AYnclkKv-lfNmA3wY7YBbb_JxRfayDTvfQfHWokHkoy %0A「。所以我無法識別選定的文件。
我怎樣才能從上面的URI的文件類型。幫我解決這個問題?
不是問題的答案和一個題外話,但我認爲,它被認爲是更好地爲不同類型的聲明不同的意圖過濾器標籤。 – hypd09
ContentResolver.getType ... – Selvin