与文件管理器共享意图文件路径以打开该路径

时间:2019-03-25 16:25:23

标签: java android android-fileprovider

我试图通过传递我要打开的文件夹的路径来打开文件管理器。

但是应用程序不断崩溃。下面我附上了代码,清单文件和崩溃日志。

如果您能够解决问题,请评论您的建议。

代码:


    Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
    Uri fileUri = FileProvider.getUriForFile(getApplicationContext(), BuildConfig.APPLICATION_ID+".fileprovider",folder);

    intent.setDataAndType(FileProvider.getUriForFile(getApplicationContext(), BuildConfig.APPLICATION_ID+".fileprovider",folder),"file/*");
    intent.setFlags(Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION);
    startActivityForResult(intent);

清单文件:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.example.fileprovideropenfile">

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />


    <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"
        tools:ignore="GoogleAppIndexingWarning">

        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="${applicationId}.fileprovider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_paths">
            </meta-data>
        </provider>
    </application>

</manifest>

XML文件:

<external-path name="file" path="com.example.fileprovideropenfile/"/>

崩溃:

E/AndroidRuntime: FATAL EXCEPTION: main
        Process: com.example.fileprovideropenfile, PID: 9483
        java.lang.IllegalStateException: Could not execute method for android:onClick
            at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:390)
            at android.view.View.performClick(View.java:6294)
            at android.view.View$PerformClick.run(View.java:24770)
            at android.os.Handler.handleCallback(Handler.java:790)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:164)
            at android.app.ActivityThread.main(ActivityThread.java:6494)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
         Caused by: java.lang.reflect.InvocationTargetException
            at java.lang.reflect.Method.invoke(Native Method)
            at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385)
            at android.view.View.performClick(View.java:6294) 
            at android.view.View$PerformClick.run(View.java:24770) 
            at android.os.Handler.handleCallback(Handler.java:790) 
            at android.os.Handler.dispatchMessage(Handler.java:99) 
            at android.os.Looper.loop(Looper.java:164) 
            at android.app.ActivityThread.main(ActivityThread.java:6494) 
            at java.lang.reflect.Method.invoke(Native Method) 
            at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) 
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) 
         Caused by: java.lang.StringIndexOutOfBoundsException: length=52; index=53
            at java.lang.String.substring(String.java:1939)
            at android.support.v4.content.FileProvider$SimplePathStrategy.getUriForFile(FileProvider.java:748)
            at android.support.v4.content.FileProvider.getUriForFile(FileProvider.java:418)
            at com.example.fileprovideropenfile.MainActivity.openTheFile(MainActivity.java:40)
            at java.lang.reflect.Method.invoke(Native Method) 
            at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385) 
            at android.view.View.performClick(View.java:6294) 
            at android.view.View$PerformClick.run(View.java:24770) 
            at android.os.Handler.handleCallback(Handler.java:790) 
            at android.os.Handler.dispatchMessage(Handler.java:99) 
            at android.os.Looper.loop(Looper.java:164) 
            at android.app.ActivityThread.main(ActivityThread.java:6494) 
            at java.lang.reflect.Method.invoke(Native Method) 
            at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) 
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

非常欢迎您发表评论。请帮我解决这个问题。

1 个答案:

答案 0 :(得分:0)

Intent intent = new Intent(Intent.ACTION_GET_CONTENT); Uri uri = Uri.parse(Environment.getExternalStorageDirectory().getPath() + "/myFolder/"); intent.setDataAndType(uri,"*/*"); startActivity(Intent.createChooser(intent, "Open folder")); 

android清单文件中没有更改。请仅使用以上代码。