我将在我的应用程序中显示pdf,并且pdf必须与应用程序捆绑在一起。
这样做的好方法是什么?
我已经读过,可以通过将pdf文件添加到res / raw文件夹并从那里读取它来实现这一点,但是当我将pdf文件放在那里时会出现项目错误。
所以我试着将pdf文件放在项目的资产文件夹中,它没有给出任何错误。
这就是我试图展示pdf的方式:
File pdfFile = new File("res/raw/file.pdf");
Uri path = Uri.fromFile(pdfFile);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(path, "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
有任何想法或建议吗?
提前致谢
答案 0 :(得分:29)
您无法从资产文件夹中直接打开 。您首先必须将文件从资源文件夹写入SD卡,然后从SD卡中读取。代码如下: -
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
File fileBrochure = new File(Environment.getExternalStorageDirectory() + "/" + "abc.pdf");
if (!fileBrochure.exists())
{
CopyAssetsbrochure();
}
/** PDF reader code */
File file = new File(Environment.getExternalStorageDirectory() + "/" + "abc.pdf");
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file),"application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
try
{
getApplicationContext().startActivity(intent);
}
catch (ActivityNotFoundException e)
{
Toast.makeText(SecondActivity.this, "NO Pdf Viewer", Toast.LENGTH_SHORT).show();
}
}
//method to write the PDFs file to sd card
private void CopyAssetsbrochure() {
AssetManager assetManager = getAssets();
String[] files = null;
try
{
files = assetManager.list("");
}
catch (IOException e)
{
Log.e("tag", e.getMessage());
}
for(int i=0; i<files.length; i++)
{
String fStr = files[i];
if(fStr.equalsIgnoreCase("abc.pdf"))
{
InputStream in = null;
OutputStream out = null;
try
{
in = assetManager.open(files[i]);
out = new FileOutputStream(Environment.getExternalStorageDirectory() + "/" + files[i]);
copyFile(in, out);
in.close();
in = null;
out.flush();
out.close();
out = null;
break;
}
catch(Exception e)
{
Log.e("tag", e.getMessage());
}
}
}
}
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);
}
}
多数民众赞成......享受!!请不要忘记给+ 1.谢谢
答案 1 :(得分:23)
如果您的应用程序实际实现了PDF阅读器,您将能够从raw/
或assets/
展示它。由于您希望它显示在单独的应用程序(例如Adobe Reader)中,我建议您执行以下操作:
assets/
目录中。openFileOutput
或getExternalFilesDir
。Intent
,除了在新创建的文件上使用getAbsolutePath()
作为意图的数据。请注意,用户可能没有PDF阅读应用程序。在这种情况下,捕获ActivityNotFoundException
并显示相应的消息非常有用。
答案 2 :(得分:1)
我使用以下格式从我自己的应用程序中打开原始资源。我还没有测试过另一个应用程序是否可以打开您的原始资源。
Uri path = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.myPdfName);
答案 3 :(得分:0)
你的pdf意图似乎很好,但你应该尝试这个来获取原始文件夹中的文件的Uri:
Uri path = Uri.parse("android.resource://<you package>/raw/<you file.pdf>");
答案 4 :(得分:0)
我的答案有各种各样的问题,所以我把一些有效的东西放在一起。
<强>布局强>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ImageView
android:id="@+id/image_pdf"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/btn_okay"
android:layout_margin="5dp"/>
<Button
android:id="@+id/btn_okay"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_margin="10dp"
android:text="@string/ok"/>
</RelativeLayout>
<强> CODE 强>
/**
* Render a page of a PDF into ImageView
* @param targetView
* @throws IOException
*/
private void openPDF(ImageView targetView) throws IOException {
//open file in assets
ParcelFileDescriptor fileDescriptor;
String FILENAME = "your.pdf";
// Create file object to read and write on
File file = new File(getActivity().getCacheDir(), FILENAME);
if (!file.exists()) {
AssetManager assetManager = getActivity().getAssets();
FileUtils.copyAsset(assetManager, FILENAME, file.getAbsolutePath());
}
fileDescriptor = ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY);
PdfRenderer pdfRenderer = new PdfRenderer(fileDescriptor);
//Display page 0
PdfRenderer.Page rendererPage = pdfRenderer.openPage(0);
int rendererPageWidth = rendererPage.getWidth();
int rendererPageHeight = rendererPage.getHeight();
Bitmap bitmap = Bitmap.createBitmap(
rendererPageWidth,
rendererPageHeight,
Bitmap.Config.ARGB_8888);
rendererPage.render(bitmap, null, null, PdfRenderer.Page.RENDER_MODE_FOR_DISPLAY);
targetView.setImageBitmap(bitmap);
rendererPage.close();
pdfRenderer.close();
}
public static boolean copyAsset(AssetManager assetManager, String fromAssetPath, String toPath) {
InputStream in = null;
OutputStream out = null;
try {
in = assetManager.open(fromAssetPath);
new File(toPath).createNewFile();
out = new FileOutputStream(toPath);
copyFile(in, out);
in.close();
in = null;
out.flush();
out.close();
out = null;
return true;
} catch(Exception e) {
e.printStackTrace();
return false;
}
}
public static 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);
}
}
答案 5 :(得分:-2)
我的应用需要在外部应用的原始数据中打开pdf文件内容... 我写道:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.OpenPdfButton);
button.setOnClickListener(new View.OnClickListener() {
InputStream is = getResources().openRawResource(R.raw.filepdf);
@Override
public void onClick(View v) {
startpdf();
}
private void startpdf() {
// TODO Auto-generated method stub
File file = new File("R.id.filepdf");
if (file.exists()) {
Uri path = Uri.fromFile(file);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(path, "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
try {
startActivity(intent);
}
catch (ActivityNotFoundException e) {
}
}
}
});
}
}