我想通过活动按钮操作从assets文件夹打开一个PDF,我使用了以下代码,但什么都没发生。
public class AssetsProvider extends ContentProvider {
@Override
public AssetFileDescriptor openAssetFile(Uri uri, String mode ) throws FileNotFoundException
{
AssetManager am = getContext( ).getAssets( );
String file_name = uri.getPath().substring(1, uri.getPath().length());
if( file_name == null )
throw new FileNotFoundException( );
AssetFileDescriptor afd = null;
try
{
afd = am.openFd( file_name );
}
catch(IOException e)
{
e.printStackTrace( );
}
return afd;
}
@Override
public String getType( Uri p1 )
{
return null;
}
@Override
public int delete( Uri p1, String p2, String[] p3 )
{
return 0;
}
@Override
public Cursor query( Uri p1, String[] p2, String p3, String[] p4, String p5 )
{
return null;
}
@Override
public Cursor query( Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder, CancellationSignal cancellationSignal )
{
return super.query( uri, projection, selection, selectionArgs, sortOrder, cancellationSignal );
}
@Override
public Uri insert( Uri p1, ContentValues p2 )
{
return null;
}
@Override
public boolean onCreate( )
{
return false;
}
@Override
public int update( Uri p1, ContentValues p2, String p3, String[] p4 )
{
return 0;
}
}
它是由如下的活动按钮操作和从这里传递的pdf文件路径url触发的:
btn_help.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
new AssetsProvider().openAssetFile(Uri.parse("file:///android_asset/Guide.pdf"),null);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
});
在Manifest上声明了提供程序类,如下所示:
<provider
android:name="com.star.form.helper.AssetsProvider"
android:authorities="om.star.form.eform.helper"
android:grantUriPermissions="true"
android:exported="true" />
请指导我。
如果需要
您可以简单地在WebView
中显示pdf文件
在您的java类中
btn_help.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent myIntent = new Intent(yourActivity.this, WebviewActivity.class);
startActivity(intent);
}
});
创建webview.xml
<?xml version = "1.0" encoding = "utf-8"?>
<LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android"
xmlns:app = "http://schemas.android.com/apk/res-auto"
xmlns:tools = "http://schemas.android.com/tools"
android:layout_width = "match_parent"
android:gravity = "center"
android:layout_height = "match_parent"
tools:context = ".MainActivity"
android:orientation = "vertical">
<WebView
android:id = "@+id/web_view"
android:layout_width = "match_parent"
android:layout_height = "match_parent" />
</LinearLayout>
和创建WebviewActivity.java
import android.app.ProgressDialog;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.EditText;
public class WebviewActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webview);
final ProgressDialog progressDialog = new ProgressDialog(this);
progressDialog.setMessage("Loading Data...");
progressDialog.setCancelable(false);
WebView web_view = findViewById(R.id.web_view);
web_view.requestFocus();
web_view.getSettings().setJavaScriptEnabled(true);
String myPdfUrl = "YOURASSETPATH";
String url = "https://docs.google.com/viewer?embedded = true&url = "+myPdfUrl;
web_view.loadUrl(url);
web_view.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
web_view.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
if (progress < 100) {
progressDialog.show();
}
if (progress = = 100) {
progressDialog.dismiss();
}
}
});
}
}