Skip to content Skip to sidebar Skip to footer

Android Show Image By Path

i want to show image in imageview without using id. i will place all images in raw folder and open try { String ss = 'res/raw/images/inrax/3150-MCM.jpg';

Solution 1:

read a stream of bytes using openRawResource()

some thing like this should work

InputStream is = context.getResources().openRawResource(R.raw.urfilename);

Check this link

http://developer.android.com/guide/topics/resources/accessing-resources.html#ResourcesFromCode

It clearly says the following

While uncommon, you might need access your original files and directories. If you do, then saving your files in res/ won't work for you, because the only way to read a resource from res/ is with the resource ID

If you want to give a file name like the one mentioned in ur code probably you need to save it on assets folder.

Solution 2:

You might be able to use Resources.getIdentifier(name, type, package) with raw files. This'll get the id for you and then you can just continue with setImageResource(id) or whatever.

intid = getResources().getIdentifier("3150-MCM", "raw", getPackageName());
if (id != 0) //if it's zero then its not valid
   image.setImageResource(id);

is what you want? It might not like the multiple folders though, but worth a try.

Solution 3:

try { // Get reference to AssetManager AssetManager mngr = getAssets();

// Create an input stream to read from the asset folderInputStreamins= mngr.open(imdir);

        // Convert the input stream into a bitmap
        img = BitmapFactory.decodeStream(ins);

  } catch (final IOException e) {
        e.printStackTrace();
  } 

here image directory is path of assets

like

assest -> image -> somefolder -> some.jpg

then path will be

image/somefolder/some.jpg

now no need of resource id for image , you can populate image on runtime using this

Post a Comment for "Android Show Image By Path"