How To Load Image From Sdcard In Android

Steps to load image from sdcard in android

  • Get the exact image path in sdcard.
  • Create Bitmap from the image path.
  • Set the bitmap into imageview.

Step 1 — Get the exact image path in sdcard

By using system intent, browse the android file system and get the exact path of the image. That you want to set in the imageview. In my case, /mnt/sdcard/DCIM/100ANDRO/DSC_0002.jpg is the path of the image in the sdcard.

Step 2 — Create Bitmap from the image path

android.graphics.BitmapFactory class provides a method decodeFile(String pathName) to decode a file path into a bitmap. decodeFile() method return the Bitmap as the response.

options.inSampleSize = 8;
final Bitmap b = BitmapFactory.decodeFile("/mnt/sdcard/DCIM/100ANDRO/DSC_0002.jpg",options)

Step 3 — Set the bitmap into imageview

Finally, set the bitmap image to the Imageview.

iv.setImageBitmap(b);

Load Image From Sdcard Example

MainActivity.Java

public class MainActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ImageView iv = (ImageView)findViewById(R.id.imageView1);
BitmapFactory.Options options = new BitmapFactory.Options();
// will results in a much smaller image than the original
options.inSampleSize = 8;
final Bitmap b = BitmapFactory.decodeFile("/mnt/sdcard/DCIM/100ANDRO/DSC_0002.jpg", options);
iv.setImageBitmap(b);
}
}

Lead Software Engineer @htcindia | @github contributor | Blog writer @howtodoandroid | Quick Learner