tags:

views:

65

answers:

3

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";
             in = new FileInputStream(ss);
        buf = new BufferedInputStream(in);
        Bitmap bMap = BitmapFactory.decodeStream(buf);
        image.setImageBitmap(bMap);
        if (in != null) {
         in.close();
        }
        if (buf != null) {
         buf.close();
        }
    } catch (Exception e) {
        Log.e("Error reading file", e.toString());
    }

but this is not working i want to access image using its path not by name

A: 

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.

Rahul
i dont want to use resource id . i will place all images in assets.
nicky
A: 

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.

int id = 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.

Espiandev
thanks for your answer .
nicky
i said i dont want to use ids,
nicky
can I ask why? Unfortunately, I think my way is the only way of "not using ids" but still having them in the raw resource directory; otherwise just put them in assets as you said.
Espiandev
A: 

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

        // Create an input stream to read from the asset folder
        InputStream ins = 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

nicky