Search code examples
androidsd-cardandroid-sdcard

accessing files on SD card in android


I want to display image from a sd card into image view

Following code works in emulator but does not work on actual phone

File f = new File(imgPath);

if (f.exists()) {
   imgView.setImageDrawable(Drawable.createFromPath("/mnt/sdcard/abc.jpg") ));

} else {

   imgView.setImageResource( R.drawable.image_abasent);

}

abc.jpg is put in emulator sd card sdk folder using DDMS I have also put abc.jpg directly on SD card through USB connection

I have added following permission too in the manifest file (but while installing from apk permission is not asked though)

uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"

Still on phone the desired image is not accessible. What else is to be added or modified?


Solution

  • Please paste your manifest and also for accessing the SDCard you must use Environment.getExternalStorageDirectory() since /mnt/sdcard/ would be a hardcoded solution and may not work on some devices or android builds.