android开发怎样获得文件夹中的所有文件
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了android开发怎样获得文件夹中的所有文件相关的知识,希望对你有一定的参考价值。
有的时候程序需要去对android的指定目录或者全局目录进行遍历获取其中的文件,但是获取文件的时候可能会遇到无法列出文件夹中的文件的问题,这就是出现的问题,对于某个子文件夹进行获取listFiles()的时候返回为NULL,也就是不允许列出文件夹中内容。这个是由于android中的安全机制的缘故,由于android继承了Linux系统的传统,对于某个特定的目录有用户的权限,一共分为三种--可读,可写,可执行;虽然说可以设置某个特定的目录的权限,但是对于目录里面的子目录和子文件都可以进行权限的设置,也就是说出了根目录权限之外,子目录本身的权限也决定了子目录可否访问,这一点需要清楚了解,所以在判断完了是否是目录之外,还需要在进行listFiles()获取File[]数据后判断获取的数组是否为空,如果为空的话,文件夹是不可访问的。样例代码如下:
01 package net.nowamagic.file;
02 import java.io.File;
03 import java.util.ArrayList;
04 import java.util.HashMap;
05 import java.util.Map;
06 import android.util.Log;
07 /**
08 * @author
09 * function 用于扫描SD卡上的文件
10 *
11 */
12 public class FileScan
13
14 private static final String TAG = "FileScan";
15 public HashMap<String, String> getMusicListOnSys(File file)
16
17 //从根目录开始扫描
18 Log.i(TAG, file.getPath());
19 HashMap<String, String> fileList = new HashMap<String, String>();
20 getFileList(file, fileList);
21 return fileList;
22
23
24 /**
25 * @param path
26 * @param fileList
27 * 注意的是并不是所有的文件夹都可以进行读取的,权限问题
28 */
29 private void getFileList(File path, HashMap<String, String> fileList)
30 //如果是文件夹的话
31 if(path.isDirectory())
32 //返回文件夹中有的数据
33 File[] files = path.listFiles();
34 //先判断下有没有权限,如果没有权限的话,就不执行了
35 if(null == files)
36 return;
37
38 for(int i = 0; i < files.length; i++)
39 getFileList(files[i], fileList);
40
41
42 //如果是文件的话直接加入
43 else
44 Log.i(TAG, path.getAbsolutePath());
45 //进行文件的处理
46 String filePath = path.getAbsolutePath();
47 //文件名
48 String fileName = filePath.substring(filePath.lastIndexOf("/")+1);
49 //添加
50 fileList.put(fileName, filePath);
51
52
53
54
出处:http://www.nowamagic.net/librarys/veda/detail/1481 参考技术A File f = new File(strPath);//strPath为路径
f.list();//String[]
真鄙视楼上这个复制党,还13级,我勒个去呀~~胡说八道,看不下去了本回答被提问者和网友采纳 参考技术B 不懂你说的什么 可以加我 我也是开发 android的
android开发怎样获取通讯录联系人信息
在Android开发中,我经常会遇到需要获取手机通讯录联系人信息,Android手机的通讯录联系人全部都存在系统的数据库中,如果须要获得通讯里联系人的信息就须要访问系统的数据库,才能将信息获取出来。下面直接贴出代码供大家参考:
程序文件java代码:
import java.io.InputStream;
import java.util.ArrayList;
import android.app.ListActivity;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.provider.ContactsContract.CommonDataKinds.Photo;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
public class ContactsActivity extends ListActivity
Context mContext = null;
/**获取库Phon表字段**/
private static final String[] PHONES_PROJECTION = new String[]
Phone.DISPLAY_NAME, Phone.NUMBER, Photo.PHOTO_ID,Phone.CONTACT_ID ;
/**联系人显示名称**/
private static final int PHONES_DISPLAY_NAME_INDEX = 0;
/**电话号码**/
private static final int PHONES_NUMBER_INDEX = 1;
/**头像ID**/
private static final int PHONES_PHOTO_ID_INDEX = 2;
/**联系人的ID**/
private static final int PHONES_CONTACT_ID_INDEX = 3;
/**联系人名称**/
private ArrayList<String> mContactsName = new ArrayList<String>();
/**联系人头像**/
private ArrayList<String> mContactsNumber = new ArrayList<String>();
/**联系人头像**/
private ArrayList<Bitmap> mContactsPhonto = new ArrayList<Bitmap>();
ListView mListView = null;
MyListAdapter myAdapter = null;
@Override
public void onCreate(Bundle savedInstanceState)
mContext = this;
mListView = this.getListView();
/**得到手机通讯录联系人信息**/
getPhoneContacts();
myAdapter = new MyListAdapter(this);
setListAdapter(myAdapter);
mListView.setOnItemClickListener(new OnItemClickListener()
@Override
public void onItemClick(AdapterView<?> adapterView, View view,
int position, long id)
//调用系统方法拨打电话
Intent dialIntent = new Intent(Intent.ACTION_CALL, Uri
.parse("tel:" + mContactsNumber.get(position)));
startActivity(dialIntent);
);
super.onCreate(savedInstanceState);
/**得到手机通讯录联系人信息**/
private void getPhoneContacts()
ContentResolver resolver = mContext.getContentResolver();
// 获取手机联系人
Cursor phoneCursor = resolver.query(Phone.CONTENT_URI,PHONES_PROJECTION, null, null, null);
if (phoneCursor != null)
while (phoneCursor.moveToNext())
//得到手机号码
String phoneNumber = phoneCursor.getString(PHONES_NUMBER_INDEX);
//当手机号码为空的或者为空字段 跳过当前循环
if (TextUtils.isEmpty(phoneNumber))
continue;
//得到联系人名称
String contactName = phoneCursor.getString(PHONES_DISPLAY_NAME_INDEX);
//得到联系人ID
Long contactid = phoneCursor.getLong(PHONES_CONTACT_ID_INDEX);
//得到联系人头像ID
Long photoid = phoneCursor.getLong(PHONES_PHOTO_ID_INDEX);
//得到联系人头像Bitamp
Bitmap contactPhoto = null;
//photoid 大于0 表示联系人有头像 如果没有给此人设置头像则给他一个默认的
if(photoid > 0 )
Uri uri =ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI,contactid);
InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(resolver, uri);
contactPhoto = BitmapFactory.decodeStream(input);
else
contactPhoto = BitmapFactory.decodeResource(getResources(), R.drawable.contact_photo);
mContactsName.add(contactName);
mContactsNumber.add(phoneNumber);
mContactsPhonto.add(contactPhoto);
phoneCursor.close();
/**得到手机SIM卡联系人人信息**/
private void getSIMContacts()
ContentResolver resolver = mContext.getContentResolver();
// 获取Sims卡联系人
Uri uri = Uri.parse("content://icc/adn");
Cursor phoneCursor = resolver.query(uri, PHONES_PROJECTION, null, null,
null);
if (phoneCursor != null)
while (phoneCursor.moveToNext())
// 得到手机号码
String phoneNumber = phoneCursor.getString(PHONES_NUMBER_INDEX);
// 当手机号码为空的或者为空字段 跳过当前循环
if (TextUtils.isEmpty(phoneNumber))
continue;
// 得到联系人名称
String contactName = phoneCursor
.getString(PHONES_DISPLAY_NAME_INDEX);
//Sim卡中没有联系人头像
mContactsName.add(contactName);
mContactsNumber.add(phoneNumber);
phoneCursor.close();
class MyListAdapter extends BaseAdapter
public MyListAdapter(Context context)
mContext = context;
public int getCount()
//设置绘制数量
return mContactsName.size();
@Override
public boolean areAllItemsEnabled()
return false;
public Object getItem(int position)
return position;
public long getItemId(int position)
return position;
public View getView(int position, View convertView, ViewGroup parent)
ImageView iamge = null;
TextView title = null;
TextView text = null;
if (convertView == null)
convertView = LayoutInflater.from(mContext).inflate(
R.layout.colorlist, null);
iamge = (ImageView) convertView.findViewById(R.id.color_image);
title = (TextView) convertView.findViewById(R.id.color_title);
text = (TextView) convertView.findViewById(R.id.color_text);
//绘制联系人名称
title.setText(mContactsName.get(position));
//绘制联系人号码
text.setText(mContactsNumber.get(position));
//绘制联系人头像
iamge.setImageBitmap(mContactsPhonto.get(position));
return convertView;
参考技术A 百度一下,网上介绍的很多的
以上是关于android开发怎样获得文件夹中的所有文件的主要内容,如果未能解决你的问题,请参考以下文章