Display image from sdcard into ImageView in android

watch_later Friday, July 29, 2016
In this tutorial we are learning about how to display image from sdcard into imageview.It is very simple and easy

Let start coding

First we have to give permission for accessing sdcard in android

add the below line in your AndroidManifest.xml file

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.forblog1"
    android:versionCode="1"
    android:versionName="1.0" >
<uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
     
    </application>

</manifest>

main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
     
        android:layout_centerInParent="true"
        android:src="@drawable/ic_launcher" />
</RelativeLayout>

MainActivity.java

import java.io.File;
import android.app.Activity;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.widget.ImageView;

public class MainActivity extends Activity {
ImageView imageView1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
imageView1=(ImageView)findViewById(R.id.imageView1);

String imagename="sample.png";
String dir = Environment.getExternalStorageDirectory()+File.separator+"Download";
File folder = new File(dir);
 File   folderpath = new File(folder+File.separator+imagename);
               if(folderpath.exists())
               {
                String folderpath1 = folderpath.getAbsolutePath().toString().trim();
                imageView1.setImageBitmap(BitmapFactory.decodeFile(folderpath1));
}
else            
{  
Log.e("","image not exists");
}
}
}
then put an image(sample.png) in sdcard -> Downlod folder   

OUTPUT:

Image will be display If sample.png file exists in sdcard -> Downlod folder

otherwise else part will be executed i.e., image not exists display in logcat





sentiment_satisfied Emoticon