要使我的默认应用程序来打开Android的csv文件

问题描述 投票:1回答:3

我想CSV文件通过我的应用程序在Android上进行opended。我把下面的代码到我的AndroidManifest,但是当我在我的Android模拟器或我的手机上安装它,并尝试从我的存储空间打开一个.csv文件,没有任何反应。

    <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.BROWSABLE" />
            <data android:scheme="file" />
            <data android:mimeType="*/*" />
            <data android:pathPattern=".*\\.csv" />
            <data android:host="*" />
        </intent-filter>
  1. 我缺少的是阻止我从我的手机打开的.csv文件?
  2. 如何将文件的URL传递给我的应用程序,所以它知道在哪里可以访问它?
android file csv types
3个回答
1
投票

我缺少的是阻止我从我的手机打开的.csv文件?

你的意图过滤器需要进行以下更改

  • 使用mime类型过滤器,而不是pathPattern,pathPattern是很难与各种可能的文件名来得到正确的,因为要打开CSV文件,最好使用mime类型过滤器“文本/ CSV”应该做的
  • 更改方案“内容”,方案文件不起作用后的Android 7

Android 7.0 Behavior Changes

共享文件应用程序之间对于应用定位的Android 7.0,Android框架强制执行禁止暴露文件StrictMode API政策://的URI您的应用程序之外。如果包含文件URI的意图离开你的应用程序,该应用程序失败,并FileUriExposedException例外。

在应用程序之间共享文件,你应该发送一个内容:// URI和授予对URI的临时访问权限。授予此权限的最简单方法是使用FileProvider类。有关权限和共享文件的详细信息,请参阅共享文件。

更改您的意向过滤器之后,它应该工作。

        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.DEFAULT" />
            <data android:scheme="content" />
            <data android:mimeType="text/csv"/>
            <data android:host="*" />
        </intent-filter>

0
投票

谢谢你的上述回应。有效!

现在,从应用程序访问文件:https://richardleggett.com/blog/2013/01/26/registering_for_file_types_in_android/

总之,在主文件中使用的OnCreate(捆绑savedInstanceState)方法如下:

Uri uri= getIntent().getData();

一旦你的URI,你可以用你平常的代码中访问该文件:

BufferedReader br = new BufferedReader(new 
InputStreamReader(getContentResolver().openInputStream(uri), "UTF-8"));

然后使用BufferedReader读取与各行:

String strLine = br.readLine();

希望这可以帮助别人!


0
投票

此代码对我的工作:

        !-- default intent filter for launching activity -->
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>

        <!-- example filter to retrieve or view plain text -->
        <!-- adds a custom title and icon for the share option -->
        <intent-filter
            android:icon="@mipmap/icon3"
            android:label="@string/app_name">

            <data android:mimeType="text/csv" />
            <data android:mimeType="text/comma-separated-values" />

            <action android:name="android.intent.action.SEND" />
            <action android:name="android.intent.action.VIEW" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
© www.soinside.com 2019 - 2024. All rights reserved.