单击列表视图项使用第三方打开Pdf文件 [英] Open Pdf file using third party on click on list view item
本文介绍了单击列表视图项使用第三方打开Pdf文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的资源文件夹中有PDF文件,现在我可以在列表视图中查看PDF文件。但现在问题是点击任何pdf我要在我的pdf查看器中打开pdf 。这是我的代码
I have PDF files in my assets folder now i am able to view that PDF files in list view .But the problem is now on click of any pdf i want to open pdf in my pdf viewer. Here is my code
<pre lang="java">public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AssetManager asset = getAssets();
try {
final String[] arrdata = asset.list("PDFfolder");
List<String> pdflist = new ArrayList<String>();
int size = arrdata.length;
for(int i = 0;i<size;i++)
{
if(arrdata[i].contains(".pdf"))
{
pdflist.add(arrdata[i]);
}
}
ArrayAdapter<String> adapter= new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,pdflist);
ListView listView = (ListView) findViewById(R.id.listView1);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
if(position == 0 ) {
File pdffile = new File("file:///android_assets/AAI.pdf");
//File ff = new File(getAssets().open("AAI.pdf"));
Uri path = Uri.fromFile(pdffile);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.setDataAndType(path, "application/pdf");
startActivity(intent);
}
}
});
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
现在请帮我解决如何使用我的资产文件夹中的意图打开它。我发现没有找到处理意图的活动的错误,因为我手机中已经有pdfviewer。
Now please help me out how to open it using intent from my assets folder. i am getting error of having no activity found to handle intent as i already have pdfviewer in my phone.
推荐答案
使用
Use
String out= Environment.getExternalStorageDirectory().getAbsolutePath() + "/X/Y/Z/" ;
File outFile = new File(out, Filename);
在您的参考编辑后。链接答案。
After Editing in your ref. Link Answer.
private void copyAssets() {
AssetManager assetManager = getAssets();
String[] files = null;
try {
files = assetManager.list("");
} catch (IOException e) {
Log.e("tag", "Failed to get asset file list.", e);
}
for(String filename : files) {
InputStream in = null;
OutputStream out = null;
try {
in = assetManager.open(filename);
String out= Environment.getExternalStorageDirectory().getAbsolutePath() + "/X/Y/Z/" ;
File outFile = new File(out, Filename);
out = new FileOutputStream(outFile);
copyFile(in, out);
in.close();
in = null;
out.flush();
out.close();
out = null;
} catch(IOException e) {
Log.e("tag", "Failed to copy asset file: " + filename, e);
}
}
}
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);
}
}
完成PDF显示后,你可以从存储中删除
After you finish showing your PDF u can just deleted from storage
这篇关于单击列表视图项使用第三方打开Pdf文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文