Load Image from sdcard

Description

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">
/*  w  w w. j a v a  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  ava  2  s  . 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  . j a  v  a2 s  .  c om*/
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.





















Home »
  Android »
    Android UI »




UI Basics
Action Bar
Animation
Button
Canvas
CheckBox
Clock Date Picker
Dialog
EditText
Event
Fragment
Gesture
GridView
ImageView
Layout
ListView
Map
Menu
Model
OpenGL
ProgressBar
RadioButton
Spinner
Tab
TextView
Thread
Toast
Video
View
WebView