没有任何一个对如何在Android中打开PDF文件的想法?我的code相本这样的:
公共类SampleActivity延伸活动{ @覆盖 保护无效的onCreate(捆绑savedInstanceState){ super.onCreate(savedInstanceState); 的setContentView(R.layout.main); CopyReadAssets(); } 私人无效CopyReadAssets(){ AssetManager assetManager = getAssets(); 在的InputStream = NULL; 出的OutputStream = NULL; 档案文件=新的文件(getFilesDir(),git.pdf); 尝试{ 在= assetManager.open(git.pdf); OUT = openFileOutput(file.getName(),Context.MODE_WORLD_READABLE); copyFile(IN,OUT); 附寄(); 在= NULL; 了out.flush(); out.close(); 出= NULL; }赶上(例外五){ Log.e(标签,e.getMessage()); } 意向意图=新意图(Intent.ACTION_VIEW); intent.setDataAndType( Uri.parse(文件://+ getFilesDir()+/git.pdf), 应用程序/ PDF格式); startActivity(意向); } 私人无效copyFile(在的InputStream,OutputStream的了)抛出IOException 字节[]缓冲区=新的字节[1024]; INT读; 而((读= in.read(缓冲))!= -1){ out.write(缓冲,0,读); } }}
解决方案
下面是code从资产的文件夹中打开PDF文件,但你必须有你的设备上安装PDF阅读器:
私人无效CopyAssets(){ AssetManager assetManager = getAssets(); 在的InputStream = NULL; 出的OutputStream = NULL; 档案文件=新的文件(getFilesDir(),fileName.pdf); 尝试{ 在= assetManager.open(fileName.pdf); OUT = openFileOutput(file.getName(),Context.MODE_WORLD_READABLE); copyFile(IN,OUT); 附寄(); 在= NULL; 了out.flush(); out.close(); 出= NULL; }赶上(例外五){ Log.e(标签,e.getMessage()); } 意向意图=新意图(Intent.ACTION_VIEW); intent.setDataAndType( Uri.parse(文件://+ getFilesDir()+/fileName.pdf), 应用程序/ PDF格式); startActivity(意向); } 私人无效copyFile(在的InputStream,OutputStream的了)抛出IOException 字节[]缓冲区=新的字节[1024]; INT读; 而((读= in.read(缓冲))!= -1){ out.write(缓冲,0,读); } }
Does any one has an idea on how to open a PDF file in Android? My code looks this this:
public class SampleActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
CopyReadAssets();
}
private void CopyReadAssets() {
AssetManager assetManager = getAssets();
InputStream in = null;
OutputStream out = null;
File file = new File(getFilesDir(), "git.pdf");
try {
in = assetManager.open("git.pdf");
out = openFileOutput(file.getName(), Context.MODE_WORLD_READABLE);
copyFile(in, out);
in.close();
in = null;
out.flush();
out.close();
out = null;
} catch (Exception e) {
Log.e("tag", e.getMessage());
}
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(
Uri.parse("file://" + getFilesDir() + "/git.pdf"),
"application/pdf");
startActivity(intent);
}
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);
}
}
}
解决方案
Here is the code for opening pdf file from asset folder, but you must have pdf reader installed on your device :
private void CopyAssets() {
AssetManager assetManager = getAssets();
InputStream in = null;
OutputStream out = null;
File file = new File(getFilesDir(), "fileName.pdf");
try {
in = assetManager.open("fileName.pdf");
out = openFileOutput(file.getName(), Context.MODE_WORLD_READABLE);
copyFile(in, out);
in.close();
in = null;
out.flush();
out.close();
out = null;
} catch (Exception e) {
Log.e("tag", e.getMessage());
}
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(
Uri.parse("file://" + getFilesDir() + "/fileName.pdf"),
"application/pdf");
startActivity(intent);
}
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);
}
}
上一篇:在Android的切断图像的边缘OpenCV的图像旋转图像、边缘、Android、OpenCV
下一篇:字典何时在 Add 或 ContainsKey 上引发 IndexOutOfRangeException?字典、Add、IndexOutOfRangeException、ContainsKey