在尝试从文件浏览器打开android应用程序时,我遗漏了什么
我已经阅读了一些关于这方面的问题和答案(包括),但大多数似乎是关于Android的旧版本,而在Android 10中,th文件系统是非常不同的。我也尝试过在Android Studio(4.0.1)中使用“应用程序链接助手”,但这似乎只适用于网站链接,我希望从文件浏览器打开我的应用程序。从我所读到的内容来看,我的意图过滤器似乎是正确的:在尝试从文件浏览器打开android应用程序时,我遗漏了什么,android,file-association,Android,File Association,我已经阅读了一些关于这方面的问题和答案(包括),但大多数似乎是关于Android的旧版本,而在Android 10中,th文件系统是非常不同的。我也尝试过在Android Studio(4.0.1)中使用“应用程序链接助手”,但这似乎只适用于网站链接,我希望从文件浏览器打开我的应用程序。从我所读到的内容来看,我的意图过滤器似乎是正确的: <activity android:name=".OpenKMZ"> <intent-filter
<activity android:name=".OpenKMZ">
<intent-filter>
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<action android:name="android.intent.action.VIEW" />
<data android:scheme="file" />
<data android:scheme="content" />
<data android:host="*" />
<!-- <data android:mimeType="application/vnd.google-earth.kmz" /> -->
<data android:pathPattern=".*\\.kmz" />
</intent-filter>
</activity>
但在安装应用程序后,文件浏览器拒绝知道如何处理.kmz文件。我在安卓8上试过ES文件浏览器,在安卓10上试过内置文件浏览器。
我已经尝试了MainActivity中包含的意图过滤器,以及此处所示的单独活动。
我是否遗漏了一些基本的东西,或者我的代码有错误?多亏了@commonware,还有这一点,我现在可以让它工作了。总共做了很多工作,只是为了绕开安卓10不允许访问常见的download文件夹这一事实! 我的清单现在包括以下内容,作为主要活动的一部分:
<intent-filter>
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<action android:name="android.intent.action.VIEW" />
<data android:scheme="file" />
<data android:scheme="content" />
<data android:host="*" />
<data android:mimeType="application/vnd.google-earth.kmz" />
</intent-filter>
请注意,“waitForDebugger”行允许您在从链接而不是从Android Studio启动应用程序时附加调试器。
“getFileFromURL()”如下所示(我将文件复制到应用程序的文件夹以备将来使用):
我希望所有这些都能帮助其他有同样问题的人。多亏了@commonware,还有这一点,我现在可以让它工作了。总共做了很多工作,只是为了绕开安卓10不允许访问常见的download文件夹这一事实! 我的清单现在包括以下内容,作为主要活动的一部分:
<intent-filter>
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<action android:name="android.intent.action.VIEW" />
<data android:scheme="file" />
<data android:scheme="content" />
<data android:host="*" />
<data android:mimeType="application/vnd.google-earth.kmz" />
</intent-filter>
请注意,“waitForDebugger”行允许您在从链接而不是从Android Studio启动应用程序时附加调试器。
“getFileFromURL()”如下所示(我将文件复制到应用程序的文件夹以备将来使用):
我希望所有这些都能帮助其他人解决同样的问题。“我是否遗漏了一些基本的东西”--自定义文件扩展名在Android中从未得到很好的支持,在现代版本的Android中几乎毫无用处。Android可能知道该MIME类型,因此您最好还原该元素并删除
pathPattern
元素。太好了,谢谢。Mime类型有效,pathPattern不存在,也不能存在<还必须使用code>scheme=“content”。现在我只需要了解如何使用生成的文件字符串content://com.google.android.apps.ndu.files.provider/2/12
-当我打开文件时,我会发布一个完整的答案,因为我需要“我缺少一些基本的东西吗”--自定义文件扩展名在Android中从未得到很好的支持,在现代版本的安卓系统中,它们几乎毫无用处。Android可能知道该MIME类型,因此您最好还原该元素并删除pathPattern
元素。太好了,谢谢。Mime类型有效,pathPattern不存在,也不能存在<还必须使用code>scheme=“content”。现在我只需要了解如何使用生成的文件字符串content://com.google.android.apps.ndu.files.provider/2/12
-当我需要打开文件时,我会发布完整答案
private void associateFile()
{
Intent intent = getIntent();
if (intent == null)
{
return;
}
Uri uri = intent.getData();
if (uri == null)
{
Toast.makeText(MainActivity.this, "null URI", Toast.LENGTH_LONG).show();
return;
}
// Debug.waitForDebugger();
kmz.getFileFromURL( this, uri);
}
public static void getFileFromURL(final Context context, final Uri uri) {
ContentResolver contentResolver = context.getContentResolver();
try {
String mimeType = contentResolver.getType(uri);
Cursor returnCursor =
contentResolver.query(uri, null, null, null, null);
int nameIndex = returnCursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
returnCursor.moveToFirst();
String fileName = returnCursor.getString(nameIndex);
InputStream inputStream = contentResolver.openInputStream(uri);
// get private downlaoad dir for Android 10
File downloadDir = context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
File f = new File(downloadDir + "/" + fileName);
FileOutputStream out = new FileOutputStream(f);
IOUtils.copyStream(inputStream,out);
.....
// then whatever you need with the file f
}catch (Exception e){
e.printStackTrace();
}