打开位于asset_folder的pdf文件

时间:2019-06-24 14:04:40

标签: java android

我必须在Android平板电脑上的pdf查看器中打开pdf文件,但是无论我如何尝试,我都会在屏幕设备上看到一条吐司消息:“无法访问文件,请验证位置”。

我的文件位于asset / www / test.pdf中: enter image description here

我遵循了许多教程,最后我在MainActivity.java中有了以下代码:

public class MainActivity extends AppCompatActivity {

    private WebView webView;
    private Context mContext;
    private AssetManager am;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        webView = findViewById(R.id.webview);
        WebSettings webSettings = webView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setDomStorageEnabled(true);
        webSettings.setAllowFileAccess(true);
        webView.setWebViewClient(new WebViewClient());
        webView.loadUrl("file:///android_asset/www/index.html");
        webView.addJavascriptInterface(new WebAppInterface(this), "Android");
    }

    @Override
    public void onBackPressed() {
        if (webView.canGoBack()) {
            webView.goBack();
        } else {
            super.onBackPressed();
        }
    }

    public class WebAppInterface {
        WebAppInterface(Context c) {
            mContext = c;
        }
        @JavascriptInterface
        public void loadPdf(String doc) {
            am = getAssets();
            InputStream in = null;
            OutputStream out = null;
            File file = new File(getFilesDir(), "filename.pdf");
            try {
                in = am.open("www/test.pdf");
                out = mContext.openFileOutput(file.getName(), Context.MODE_PRIVATE);
                copyFile(in, out);
                in.close();
                in = null;
                out.close();
                out = null;
            } catch (Exception e) {
                Log.e("tag", e.getMessage());
            }
//            File file = new File(Environment.getExternalStorageDirectory() + "/www/test.pdf");
            Uri excelPath;
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                excelPath = FileProvider.getUriForFile(mContext, BuildConfig.APPLICATION_ID + ".provider", file);
            } else {
                excelPath = Uri.fromFile(file);
            }
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setDataAndType(excelPath, "application/pdf");
//            intent.setPackage("com.adobe.reader");
//            intent.setDataAndType(Uri.parse("file://" + getFilesDir() + "filename.pdf"), "application/pdf");
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            try {
                startActivity(intent);
            } catch (ActivityNotFoundException e) {
                Toast.makeText(mContext, "Please install Adobe Reader", Toast.LENGTH_SHORT).show();
            }
        }
    }

    private void copyFile(InputStream in, OutputStream out) throws IOException {
        byte[] buffer = new byte[1024];
        int read;
        while ((read = in.read(buffer)) != -1) {
            out.write(buffer, 0, read);
        }
    }
}

这是我的AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.myapplication">

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <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">
        <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="androidx.core.content.FileProvider"
            android:authorities="${applicationId}.provider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/provider_paths"/>
        </provider>
    </application>

</manifest>

还有我的provider_paths.xml:

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path
        name="external"
        path="." />
    <external-files-path
        name="external_files"
        path="." />
    <cache-path
        name="cache"
        path="." />
    <external-cache-path
        name="external_cache"
        path="." />
    <files-path
        name="files"
        path="." />
    <root-path
        name="root"
        path="." />
</paths>

我在build / build APK中生成一个APK,并将其粘贴到我的设备上,所以不知道这是否是正确的方法。 我读了许多教程和stackoverflow问题,但没有解决方案适合我,控制台中没有错误消息,也不知道我在做什么错。

0 个答案:

没有答案