title: android custom listview with gallery images
body: <p>I'm trying to add all the images contained in a folder in my custom listview, where I show a thumbnail of the photo and a little description.
  I've found a lot of examples showing images from URL or drawable folder in Android Studio, but nothing working for me that loads a picture located in /storage/emulated/0/DCIM/Camera/AAAAMMGG_HHMMSS.jpg</p>
  
  <p>Here is my code - MainActivity:</p>
  
  <pre><code>import android.content.ContentValues;
  public class MainActivity extends ActionBarActivity implements OnClickListener {
  
  public int PHOTO_REQUEST_CODE = 1;
  private Uri fileUri;
  private ArrayAdapter&lt;String&gt; adapter;
  
  TextView ceScanResults;
  ImageButton btnScan;
  
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      // Set a toolbar to replace the action bar.
      Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
      setSupportActionBar(toolbar);
      View addButton = findViewById(mci);
      ArrayList&lt;ListItem&gt; listData = getListData();
  
      final ListView listView = (ListView) findViewById(R.id.custom_list);
      listView.setAdapter(new CustomListAdapter(this, listData));
      listView.setOnItemClickListener(new OnItemClickListener() {
  
          @Override
          public void onItemClick(AdapterView&lt;?&gt; a, View v, int position, long id) {
              ListItem newsData = (ListItem) listView.getItemAtPosition(position);
              Toast.makeText(MainActivity.this, "Selected :" + " " + newsData, Toast.LENGTH_LONG).show();
          }
      });
      initViews();
  }
  
  private ArrayList&lt;ListItem&gt; getListData() {
      ArrayList&lt;ListItem&gt; listMockData = new ArrayList&lt;ListItem&gt;();
      String[] images = getResources().getStringArray(R.array.images_array);
      String[] headlines = getResources().getStringArray(R.array.headline_array);
  
      for (int i = 0; i &lt; images.length; i++) {
          ListItem newsData = new ListItem();
          newsData.setUrl(images[i]);
          newsData.setHeadline(headlines[i]);
          newsData.setReporterName("CE code");
          newsData.setDate("28/07/2015 - 10:31");
          listMockData.add(newsData);
      }
      return listMockData;
  }
  
  private void initViews() {
      //ceScanResults = (TextView) findViewById(R.id.ceResults);
      btnScan = (ImageButton) findViewById(R.id.mci);
      btnScan.setOnClickListener(this);
  }
  
  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
      //getMenuInflater().inflate(R.menu.main, menu);
      return true;
  }
  
  private void initGalleryFetchImage() {
      Intent i = new Intent(
              Intent.ACTION_PICK,
              android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
      startActivityForResult(i, RESULT_OK);
  }
  
  @Override
  public void onActivityResult(int requestCode, int resultCode, Intent intent) {
      super.onActivityResult(requestCode, resultCode, intent);
      Uri selectedImageUri = null;
      String filePath = null;
      if (requestCode == 1 &amp;&amp; resultCode == RESULT_OK ) {
          Uri selectedImage = intent.getData();
          String[] filePathColumn = { MediaStore.Images.Media.DATA };
  
          try {
              Bitmap bmp = BitmapFactory.decodeStream(getContentResolver()
                      .openInputStream(selectedImage));
          } catch (FileNotFoundException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
      }
      if (resultCode == RESULT_OK) {
  
          if (requestCode == 1) {
  
              if (resultCode == RESULT_OK) {
                  //use imageUri here to access the image
                  selectedImageUri = fileUri;
  
              } else if (resultCode == RESULT_CANCELED) {
                  Toast.makeText(this, "Picture was not taken", Toast.LENGTH_SHORT).show();
              } else {
                  Toast.makeText(this, "Picture was not taken", Toast.LENGTH_SHORT).show();
              }
  
          } else if (requestCode == 2) {
  
              selectedImageUri = intent.getData();
  
          }
  
          if(selectedImageUri != null){
              try {
                  // OI FILE Manager
                  String filemanagerstring = selectedImageUri.getPath();
  
                  // MEDIA GALLERY
                  String selectedImagePath = getPath(selectedImageUri);
  
                  if (selectedImagePath != null) {
                      filePath = selectedImagePath;
                  } else if (filemanagerstring != null) {
                      filePath = filemanagerstring;
                  } else {
                      Toast.makeText(getApplicationContext(), "Unknown path",
                              Toast.LENGTH_LONG).show();
                      Log.e("Bitmap", "Unknown path");
                  }
  
                  if (filePath != null) {
                      decodeFile(filePath);
                  } else {
                      // bitmap = null;
                  }
              } catch (Exception e) {
                  Toast.makeText(getApplicationContext(), "Internal error",
                          Toast.LENGTH_LONG).show();
                  Log.e(e.getClass().getName(), e.getMessage(), e);
              }
          }
      }
  }
  
  public String getPath(Uri uri) {
      String res = null;
      String[] proj = { MediaStore.Images.Media.DATA };
      Cursor cursor = getContentResolver().query(uri, proj, null, null, null);
      if(cursor.moveToFirst()){;
          int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
          res = cursor.getString(column_index);
      }
      cursor.close();
      return res;
  }
  
  @Override
  public void onClick(View v) {
      if (v.getId() == R.id.mci) {
          Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
          intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
          startActivityForResult(intent, PHOTO_REQUEST_CODE);
          String fileName = "new-photo-name.jpg";
          //create parameters for Intent with filename
          ContentValues values = new ContentValues();
          values.put(MediaStore.Images.Media.TITLE, fileName);
          values.put(MediaStore.Images.Media.DESCRIPTION,"Image captured by camera");
          //imageUri is the current activity attribute, define and save it for later usage (also in onSaveInstanceState)
          //create new Intent
          startActivityForResult(intent, 1);
          fileUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
  
      }
  }
  public void decodeFile(String filePath) {
      // Decode image size
      BitmapFactory.Options o = new BitmapFactory.Options();
  
      o.inJustDecodeBounds = true;
      try ( InputStream is = new URL(filePath).openStream() ) {
          Bitmap bitmap = BitmapFactory.decodeStream( is );
      } catch (MalformedURLException e) {
          e.printStackTrace();
      } catch (IOException e) {
          e.printStackTrace();
      }
      // The new size we want to scale to
      final int REQUIRED_SIZE = 1024;
      // Find the correct scale value. It should be the power of 2.
      int width_tmp = o.outWidth, height_tmp = o.outHeight;
      int scale = 1;
      while (true) {
          if (width_tmp &lt; REQUIRED_SIZE &amp;&amp; height_tmp &lt; REQUIRED_SIZE)
              break;
          width_tmp /= 2;
          height_tmp /= 2;
          scale *= 2;
      }
  }
  }
  </code></pre>
  
  <p>And the code of my CustomListAdapter:</p>
  
  <pre><code>    public class CustomListAdapter extends BaseAdapter {
      private ArrayList&lt;ListItem&gt; listData;
      private LayoutInflater layoutInflater;
  
      public CustomListAdapter(Context context, ArrayList&lt;ListItem&gt; listData) {
          this.listData = listData;
          layoutInflater = LayoutInflater.from(context);
      }
  
      @Override
      public int getCount() {
          return listData.size();
      }
  
      @Override
      public Object getItem(int position) {
          return listData.get(position);
      }
  
      @Override
      public long getItemId(int position) {
          return position;
      }
  
      public View getView(int position, View convertView, ViewGroup parent) {
          ViewHolder holder;
          if (convertView == null) {
              convertView = layoutInflater.inflate(R.layout.list_row_layout, null);
              holder = new ViewHolder();
              holder.headlineView = (TextView) convertView.findViewById(R.id.title);
              holder.reporterNameView = (TextView) convertView.findViewById(R.id.reporter);
              holder.reportedDateView = (TextView) convertView.findViewById(R.id.date);
              holder.imageView = (ImageView) convertView.findViewById(R.id.thumbImage);
              convertView.setTag(holder);
  
          } else {
              holder = (ViewHolder) convertView.getTag();
          }
  
          ListItem newsItem = listData.get(position);
          holder.headlineView.setText(newsItem.getHeadline());
          holder.reporterNameView.setText("" + newsItem.getReporterName());
          holder.reportedDateView.setText(newsItem.getDate());
  
          if (holder.imageView != null) {
              new ImageDownloaderTask(holder.imageView).execute(newsItem.getUrl());
          }
  
          return convertView;
      }
  
      static class ViewHolder {
          TextView headlineView;
          TextView reporterNameView;
          TextView reportedDateView;
          ImageView imageView;
      }
      }
  
  My class ImageDownloaderTask:
  
      class ImageDownloaderTask extends AsyncTask&lt;String, Void, Bitmap&gt; {
  
      private final WeakReference&lt;ImageView&gt; imageViewReference;
  
      public ImageDownloaderTask(ImageView imageView) {
          imageViewReference = new WeakReference&lt;ImageView&gt;(imageView);
      }
  
      @Override
      protected Bitmap doInBackground(String... params) {
          return downloadBitmap(params[0]);
      }
  
      @Override
      protected void onPostExecute(Bitmap bitmap) {
          if (isCancelled()) {
              bitmap = null;
          }
  
          if (imageViewReference != null) {
              ImageView imageView = imageViewReference.get();
              if (imageView != null) {
                  if (bitmap != null) {
                      imageView.setImageBitmap(bitmap);
                  } else {
                      Drawable placeholder = imageView.getContext().getResources().getDrawable(R.drawable.placeholder);
                      imageView.setImageDrawable(placeholder);
                  }
              }
  
          }
      }
  
      private Bitmap downloadBitmap(String url) {
          HttpURLConnection urlConnection = null;
          try {
              URL uri = new URL(url);
              urlConnection = (HttpURLConnection) uri.openConnection();
  
              int statusCode = urlConnection.getResponseCode();
              if (statusCode != HttpStatus.SC_OK) {
                  return null;
              }
  
              InputStream inputStream = urlConnection.getInputStream();
              if (inputStream != null) {
                  Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
                  return bitmap;
              }
          } catch (Exception e) {
              urlConnection.disconnect();
              Log.w("ImageDownloader", "Error downloading image from " + url);
          } finally {
              if (urlConnection != null) {
                  urlConnection.disconnect();
              }
          }
          return null;
      }
      }
  </code></pre>
  
  <p>My ListItem.xml:</p>
  
  <pre><code>    &lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      &gt;
  
      &lt;ImageView
          android:id="@+id/image"
          android:layout_width="40dp"
          android:layout_height="40dp"
          android:layout_margin="5dp"
          android:contentDescription="icon"
          /&gt;
  
      &lt;TextView
          android:id="@+id/title"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:textAppearance="?android:attr/textAppearanceMedium"
          /&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>Thanks a lot for your precious support!
  Greetings,
  Michele.</p>