0
I have a problem listing the photos in the folder DCIM
on Android. It gives loop error for
, launching a java.lang.NullPointerException
? What can it be?
See the code that I get 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 class Adapter:
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;
}
}
http://answall.com/help/mcve
– Renan Gomes
I moved the folder to SDCARD and still the problem persists in FOR.
– user25894
Felipe when Voce does this 'String root_sd = Environment.getExternalStorageDirectory(). toString();' you get the path your sdcard has from the device if your images are inside the DCIM keep that way, but if there is no folder in your sdcard it will happen as @Victorstafusa said.
– Wellington Avelino