[英]How to open any type of file from recycler view adapter class?
What i'm trying to do is when the file is successfully downloaded it should be automatically opened from the file path but now here the problem is that i need to fire the path from my recycler view adapter class when i try to do that it generates an exception Take a look at my adapter class: Here i'm using a class with the adapter which will basically download the file from the url but when i try to fire the intent for opening the file from download class it just goes to error something went wrong. 我想做的是,当文件成功下载后,应该从文件路径中自动打开它,但是现在这里的问题是,当我尝试执行该操作时,我需要从回收者视图适配器类中触发该路径一个例外,看看我的适配器类:在这里,我正在使用一个带有适配器的类,该类基本上将从URL下载文件,但是当我尝试激发从下载类打开文件的意图时,它就会出错出错。
public class Filelistadapter extends RecyclerView.Adapter<Filelistadapter.ProjectViewHolder>{
private String url;
private ArrayList<Taskmsg> dataList;
Context mContext;
public Filelistadapter(ArrayList<Taskmsg> dataList, Context mContext) {
this.dataList = dataList;
this.mContext=mContext;
}
@Override
public ProjectViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
View view = layoutInflater.inflate(R.layout.file_item, parent, false);
return new ProjectViewHolder(view);
}
@Override
public void onBindViewHolder(ProjectViewHolder holder, int position) {
String fname=dataList.get(position).getFilename();
holder.txtfilename.setText(fname);
holder.download.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
url="http://pms.cloudester.com/task_doc/"+fname;
new DownloadFile().execute(url);
}
});
}
@Override
public int getItemCount() {
return dataList.size();
}
class ProjectViewHolder extends RecyclerView.ViewHolder {
TextView txtfilename;
Button download;
ProjectViewHolder(View itemView) {
super(itemView);
txtfilename = (TextView) itemView.findViewById(R.id.tvfilename);
download=itemView.findViewById(R.id.actionButton);
}
}
private class DownloadFile extends AsyncTask<String, String, String> {
private ProgressDialog progressDialog;
private String fileName;
private String folder;
private boolean isDownloaded;
/**
* Before starting background thread
* Show Progress Bar Dialog
*/
@Override
protected void onPreExecute() {
super.onPreExecute();
this.progressDialog = new ProgressDialog(mContext);
this.progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
this.progressDialog.setCancelable(false);
this.progressDialog.show();
}
/**
* Downloading file in background thread
*/
@Override
protected String doInBackground(String... f_url) {
int count;
try {
URL url = new URL(f_url[0]);
URLConnection connection = url.openConnection();
connection.connect();
// getting file length
int lengthOfFile = connection.getContentLength();
// input stream to read file - with 8k buffer
InputStream input = new BufferedInputStream(url.openStream(), 8192);
String timestamp = new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss").format(new Date());
//Extract file name from URL
fileName = f_url[0].substring(f_url[0].lastIndexOf('/') + 1, f_url[0].length());
//Append timestamp to file name
fileName = timestamp + "_" + fileName;
//External directory path to save file
folder = Environment.getExternalStorageDirectory() + File.separator + "pms/";
//Create androiddeft folder if it does not exist
File directory = new File(folder);
if (!directory.exists()) {
directory.mkdirs();
}
// Output stream to write file
OutputStream output = new FileOutputStream(folder + fileName);
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
// publishing the progress....
// After this onProgressUpdate will be called
publishProgress("" + (int) ((total * 100) / lengthOfFile));
// writing data to file
output.write(data, 0, count);
}
// flushing output
output.flush();
// closing streams
output.close();
input.close();
return "Downloaded at: " + folder + fileName;
} catch (Exception e) {
Log.e("Error: ", e.getMessage());
}
return "Something went wrong";
}
/**
* Updating progress bar
*/
protected void onProgressUpdate(String... progress) {
// setting progress percentage
progressDialog.setProgress(Integer.parseInt(progress[0]));
}
@Override
protected void onPostExecute(String message) {
// dismiss the dialog after the file was downloaded
this.progressDialog.dismiss();
// Display File path after downloading
Toast.makeText(mContext,
message, Toast.LENGTH_LONG).show();
}
}
}
You have two errors 你有两个错误
Do this inside button click or else you will get the name of last postion always 在按钮内单击执行此操作,否则将始终获得上一个位置的名称
String fname=dataList.get(position).getFilename(); 字符串fname = dataList.get(position).getFilename();
And Do this in post execute method change type as you want 并根据需要在后执行方法更改类型中执行此操作
Intent testIntent = new Intent();
testIntent.setType("application/pdf");
testIntent.setAction(Intent.ACTION_VIEW);
Uri uri = Uri.fromFile(folder+filename);
testIntent.setDataAndType(uri, "application/pdf");
startActivity(testIntent);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.