Android UI How to - Load Image from sdcard








We can Load Image from sdcard and use them in ImageView.

Example

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">
//  ww w. ja  va 2 s .com
    <ImageView
        android:id="@+id/image3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

The following Java code sets the image from resource.

package com.java2s.app;
//  ww  w .  j a  va  2s.c o  m
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.widget.ImageView;

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ImageView imgView = (ImageView)findViewById(R.id.image3);

        imgView.setImageDrawable(Drawable.createFromPath("/mnt/sdcard/myImage.jpg") );
    }

}
  




Example 2

The following setter method takes the URI of an image file and uses that as the image source.

package com.java2s.app;
/*w  w  w  . ja v a2 s .c o m*/
import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.widget.ImageView;

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ImageView imgView = (ImageView) findViewById(R.id.image3);

        imgView.setImageURI(Uri.parse("file://mnt/sdcard/myImage.jpg"));
    }

}

This method is really only intended to be used for local images on the device, not for images that you might find through HTTP.

To use Internet-based images as the source for your ImageView, you'd most likely use BitmapFactory and an InputStream.