我正在尝试创建一个项目,其中我需要我的应用程序来读取本地存储的pdf,doc,xls文件。
我对如何做到这一点一无所知。我想在我的webview中显示所有本地存储的文件。是否有任何库来呈现此文件或以任何其他方式执行此操作?如果有人可以指导我,那将是我的荣幸。
答案 0 :(得分:0)
package com.cdn.file;
import java.io.File;
import java.io.FilenameFilter;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class FileExplorerActivity extends Activity {
/** Called when the activity is first created. */
private Button buttonList, buttonBack;
// Stores names of traversed directories
ArrayList<String> str = new ArrayList<String>();
// Check if the first level of the directory structure is the one showing
private static final String TAG = "F_PATH";
private List<Item> fileList;
private Item item;
private File path = new File(Environment.getExternalStorageDirectory() + "");
private String chosenFile = "";
ListView listViewDir;
ListAdapter adapter;
ListFile listFileAdaptor;
int value = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
listViewDir = (ListView) findViewById(R.id.listViewDir);
buttonBack = (Button) findViewById(R.id.buttonBack);
buttonBack.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
buttonList = (Button) findViewById(R.id.buttonList);
buttonList.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (isSdPresent()) {
Toast.makeText(FileExplorerActivity.this, "Un Mounted",
Toast.LENGTH_SHORT).show();
loadFileList();
listFileAdaptor = new ListFile(FileExplorerActivity.this,
fileList);
listViewDir.setAdapter(listFileAdaptor);
}
else {
Toast.makeText(FileExplorerActivity.this, "Mounted",
Toast.LENGTH_SHORT).show();
}
}
});
listViewDir.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// if (value == 0) {
chosenFile = fileList.get(position).getFile();
/*} else {
chosenFile = backList.get(position).getFile();
value =0;
}*/
File sel = new File(path + "/" + chosenFile);
Log.v("Prashant111 ==", sel.toString());
Log.v("Prashant222 ==", "" + sel.toURI());
if (sel.isDirectory()) {
// Adds chosen directory to list
str.add(chosenFile);
path = new File(sel + "");
loadFileList();
listFileAdaptor = new ListFile(FileExplorerActivity.this,
fileList);
listViewDir.setAdapter(listFileAdaptor);
// Log.d(TAG, path.getAbsolutePath());
Log.d("Prashant333 ==", "" + path.toURI());
Log.d("Prashant444 ==", "" + path.toString());
}
// Checks if 'up' was clicked
// File picked
else {
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri uri = Uri.fromFile(new File(chosenFile));
intent.putExtra(Intent.ACTION_VIEW, uri);
Intent chooser = Intent.createChooser(intent, "Prashant");
startActivity(chooser);
}
}
});
}
public static boolean isSdPresent() {
return android.os.Environment.getExternalStorageState().equals(
android.os.Environment.MEDIA_MOUNTED);
}
private class Item {
public String file;
public int icon;
public String getFile() {
return file;
}
public void setFile(String file) {
this.file = file;
}
public int getIcon() {
return icon;
}
public void setIcon(int icon) {
this.icon = icon;
}
}
private void loadFileList() {
try {
path.mkdirs();
} catch (SecurityException e) {
Log.e(TAG, "unable to write on the sd card ");
}
// Checks whether path exists
if (path.exists()) {
FilenameFilter filter = new FilenameFilter() {
@Override
public boolean accept(File dir, String filename) {
File sel = new File(dir, filename);
// Filters based on whether the file is hidden or not
return (sel.isFile() || sel.isDirectory())
&& !sel.isHidden();
}
};
String[] fList = path.list(filter);
fileList = new ArrayList<FileExplorerActivity.Item>();
for (int i = 0; i < fList.length; i++) {
item = new Item();
item.setIcon(R.drawable.file_icon);
item.setFile(fList[i]);
// fileList.add(item);
// Convert into file path
File sel = new File(path, fList[i]);
Log.v("sel ====", sel.toString());
String name = sel.getName();
// Set drawables
if (sel.isDirectory()) {
item.setFile(name);
item.setIcon(R.drawable.directory_icon);
fileList.add(item);
} else {
if (name.endsWith(".mp4") || name.endsWith(".3gp")) {
Log.v("aaaaaaaaaaa", name);
item.setFile(name);
item.setIcon(R.drawable.icon);
fileList.add(item);
} else {
fileList.remove(item);
}
}
}
} else {
Log.e(TAG, "path does not exist");
}
}
class ViewHolder {
TextView textViewName;
ImageView view;
}
class ListFile extends BaseAdapter {
List<Item> fileList;
private LayoutInflater inflator;
Context context;
ListFile(Context context, List<Item> fileList) {
this.fileList = fileList;
this.context = context;
inflator = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return fileList.size();
}
@Override
public Object getItem(int position) {
return fileList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
if (getItemViewType(position) == 0) {
convertView = inflator.inflate(R.layout.file_list, null);
}
holder = new ViewHolder();
holder.textViewName = (TextView) convertView
.findViewById(R.id.textView1);
holder.view = (ImageView) convertView
.findViewById(R.id.imageView1);
convertView.setTag(holder);
// Set the display text
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.textViewName.setText(fileList.get(position).getFile());
holder.view.setImageResource((fileList.get(position).getIcon()));
return convertView;
}
}
}
尝试使用此代码。我正在拍摄视频文件,但您可以指定所需的文件类型。