当我点击按钮时,我想从filepath
获得filename
。我正在使用自定义BaseAdapter,因为我正在传递ArrayList<String>
。我正在获取文件名,但我也需要sdcard的绝对文件路径。这是我在getView();
String mFileName = myitem.get(position);
viewHolder.bt_encrypt.setTag(mFileName);
在按钮的onclick上,当我点击同一行的按钮时,我在下面的一段代码中添加了一个文件
String fileName = (String) v.getTag();
从这个如何获取文件路径?
完整代码
private void getDir(String dirPath){
item = new ArrayList<String>();
path = new ArrayList<String>();
File f = new File(dirPath);
File[] files = f.listFiles();
if(!dirPath.equals(MainActivity.root_sd_path)){
item.add(MainActivity.root_sd_path);
path.add(MainActivity.root_sd_path);
item.add("../");
path.add(f.getParent());
}
for(int i=0; i < files.length; i++)
{
File file = files[i];
if(!file.isHidden() && file.canRead()){
path.add(file.getAbsolutePath());
if(file.isDirectory()){
item.add(file.getName() + "/");
}else{
item.add(file.getName());
}
}
}
SDcardFiles sdcardFilesAdapter = new SDcardFiles(item, dirPath);
setListAdapter(sdcardFilesAdapter);
}
public class SDcardFiles extends BaseAdapter{
private LayoutInflater mInflator;
String myfilename;
ArrayList<String> myitem;
File sdFile;
private String mFileName;
public SDcardFiles(ArrayList<String> fileItem, String fileDirpath){
myitem = new ArrayList<String>();
myitem = fileItem;
mInflator = MyFiles.this.getLayoutInflater();
@Override
public int getCount() {
return myitem.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return myitem.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View view, ViewGroup parent) {
ViewHolder viewHolder;
if (view == null) {
view = mInflator.inflate(R.layout.myfiles_sdfiles, null);
viewHolder = new ViewHolder();
viewHolder.fileName = (TextView)view.findViewById(R.id.file_name);
viewHolder.bt_encrypt = (Button)view.findViewById(R.id.encrypt);
view.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) view.getTag();
}
mFileName = myitem.get(position);
viewHolder.bt_encrypt.setTag(mFileName);
String myFileName = mFileName;
System.out.println("mFile====>"+myFileName);
if(myFileName != null && myFileName.length() > 0){
viewHolder.fileName.setText(myFileName.toString());
}
viewHolder.bt_encrypt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String fileName = (String) v.getTag();
System.out.println("inside encryption====>"+fileName);
// Here I need filepath also
}
感谢
答案 0 :(得分:-1)
你可以尝试这样的事情
Environment.getExternalStorageDirectory()getAbsolutePath();