I have a problem listing the photos in the DCIM
folder on Android. Does it give the loop error for
, casting java.lang.NullPointerException
? What can it be?
See the code that takes the information from the folder:
public class MainActivity extends Activity {
private ListView listView;
private List<String> minhaLista;
private File file;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//carrega o layout onde contem o ListView
setContentView(R.layout.layout_arquivos);
minhaLista = new ArrayList<String>();
String root_sd = Environment.getExternalStorageDirectory().toString();
file = new File(root_sd + "/DCIM");
File list[] = file.listFiles();
System.out.println(file);
for( int i=0; i< list.length; i++)
{
minhaLista.add( list[i].getName() );
}
AdapterListView adapter = new AdapterListView(this, minhaLista);
listView = (ListView)findViewById(R.id.list);
listView.setAdapter(adapter);
}
}
The adapter class:
public class AdapterListView extends BaseAdapter {
private Context mContext;
private List<String> mArquivos;
public AdapterListView(Context context, List<String> arquivos) {
// TODO Auto-generated constructor stub
mContext = context;
mArquivos = arquivos;
}
public int getCount() {
return mArquivos.size(); // Retorna o número de linhas
}
public String getItem(int position) {
return mArquivos.get(position); // Retorna o item daquela posição
}
public long getItemId(int position) {
return position;
}
// Classe responsável por guardar a referência da View
static class ViewHolder {
TextView hTextView;
}
public View getView(int position, View convertView, ViewGroup p) {
ViewHolder holder = null;
String item = this.getItem(position);
if (convertView == null) {
convertView = LayoutInflater.from(mContext).inflate(R.layout.item_escolher_pasta, p, false);
holder = new ViewHolder();
holder.hTextView = (TextView) convertView.findViewById(R.id.txtNomePastas);
convertView.setTag(holder);
} else {
holder = (ViewHolder)convertView.getTag();
}
holder.hTextView.setText(item);
return convertView;
}
}