Android 使用第三方点击列表视图项打开Pdf文件
我的资产文件夹中有PDF文件,现在我可以在列表视图中查看该PDF文件。但问题是现在单击任何我想在PDF查看器中打开的PDF文件。 这是我的密码Android 使用第三方点击列表视图项打开Pdf文件,android,listview,pdf,assets,Android,Listview,Pdf,Assets,我的资产文件夹中有PDF文件,现在我可以在列表视图中查看该PDF文件。但问题是现在单击任何我想在PDF查看器中打开的PDF文件。 这是我的密码 public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setCo
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();
}
}
}
公共类MainActivity扩展活动{
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AssetManager资产=getAssets();
试一试{
最终字符串[]arrdata=asset.list(“PDFfolder”);
List pdflist=new ArrayList();
int size=arrdata.length;
对于(int i=0;i您需要安装一个应用程序来处理打开PDF的操作。您应该使用意图选择器,如下所示:
File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() +"/"+ filename);
Intent target = new Intent(Intent.ACTION_VIEW);
target.setDataAndType(Uri.fromFile(file),"application/pdf");
target.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
Intent intent = Intent.createChooser(target, "Open File");
try {
startActivity(intent);
} catch (ActivityNotFoundException e) {
// Instruct the user to install a PDF reader here, or something
}
资产目录中的文件不会解压缩,而是直接从APK ZIP文件读取
所以,你真的不能让需要一个文件的东西接受一个资产“文件”
相反,您必须提取资产并将其写入单独的文件,如下所示:
File f = new File(getCacheDir()+"/fileName.pdf");
if (!f.exists()) try {
InputStream is = getAssets().open("fileName.pdf");
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
FileOutputStream fos = new FileOutputStream(f);
fos.write(buffer);
fos.close();
} catch (Exception e) { throw new RuntimeException(e); }
您还可以使用来管理资产文件夹中的文件。在“活动”中,调用getAssets()
方法以获取AssetManager
实例
希望它能对您有所帮助。在我的项目中工作得很好。请尝试此代码
/**
* get on item click listener
*/
userList.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View v,
final int position, long id) {
TextView tv_paper_name = (TextView) v.findViewById(R.id.tv_paper_name);
String PaperName = tv_paper_name.getText().toString();
File extStore = Environment.getExternalStorageDirectory();
File myFile = new File(extStore.getAbsolutePath() + "/Exam Papers/"+PaperName+".pdf");
if (myFile.exists()) {
File pdfFile = new File(Environment.getExternalStorageDirectory() + "/Exam Papers/" +PaperName+".pdf"); // -> filename
Uri path = Uri.fromFile(pdfFile);
Intent pdfIntent = new Intent(Intent.ACTION_VIEW);
pdfIntent.setDataAndType(path, "application/pdf");
pdfIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
try {
startActivity(pdfIntent);
} catch (ActivityNotFoundException e) {
Toast.makeText(MainActivity.this, "No Application available to view PDF", Toast.LENGTH_SHORT).show();
}
}else{
Toast.makeText(MainActivity.this, "Download file first", Toast.LENGTH_SHORT).show();
}
}
});
/**
*获取项目单击侦听器
*/
setOnItemClickListener(新的OnItemClickListener(){
@凌驾
public void onItemClick(AdapterView父视图、视图v、,
最终整数位置,长id){
TextView tv\u paper\u name=(TextView)v.findViewById(R.id.tv\u paper\u name);
String PaperName=tv_paper_name.getText().toString();
文件extStore=Environment.getExternalStorageDirectory();
File myFile=新文件(extStore.getAbsolutePath()+“/examples/”+PaperName+“.pdf”);
如果(myFile.exists()){
File pdfFile=新文件(Environment.getExternalStorageDirectory()+“/examples/”+PaperName+“.pdf”);//->filename
Uri路径=Uri.fromFile(pdfFile);
Intent pdfIntent=新意图(Intent.ACTION\u视图);
setDataAndType(路径,“application/pdf”);
pdfIntent.setFlags(意图、标志、活动、清除、顶部);
试一试{
星触觉;
}捕获(ActivityNotFounde异常){
Toast.makeText(MainActivity.this,“没有可查看PDF的应用程序”,Toast.LENGTH_SHORT.show();
}
}否则{
Toast.makeText(MainActivity.this,“首先下载文件”,Toast.LENGTH_SHORT.show();
}
}
});
尝试回答此链接中给出的答案,但您给出的链接在SD卡中有pdf文件,但我的文件在assets文件夹中…使用这些链接修改文件路径请检查我编辑的部分,这样说明文件路径无效。不提取是不可能的…因为我在这里读过,他们也没有给出解决方案can请看我的代码,告诉我我需要做什么?任何帮助都会很感激的谢谢@Shruti…但是我在列表视图中有pdf名称,我必须在单击不同的名称时打开不同的pdf。。。。