2

My application files are bellow, I want to get sharethis.png file in java code , What is syntax ?

enter image description here

I used this ...

Bitmap bMap = BitmapFactory.decodeFile("file:///android_asset/www/sharethis.png");

but not working

Adnan Abdollah Zaki
  • 3,938
  • 5
  • 48
  • 55
suraj mahajan
  • 804
  • 1
  • 10
  • 20

4 Answers4

1

Try:

Bitmap shareThis = BitmapFactory.decodeResource(context.getResources(),
                                       R.drawable.shareThis);
Karan
  • 2,094
  • 14
  • 25
1

If you want to reference it from a JavaScript or html file in your assets folder, use file:///android_asset/www/sharethis.jpg.

Otherwise, according to this answer, this will list all the files in the assets folder:

AssetManager assetManager = getAssets();
String[] files = assetManager.list("");

This to open a certain file:

InputStream input = assetManager.open(assetName);
Community
  • 1
  • 1
Anyonymous2324
  • 240
  • 3
  • 12
1

Try this:

 try {

    // get input stream

    InputStream ims = getAssets().open("sharethis.png");

    // load image as Drawable

    Drawable d = Drawable.createFromStream(ims, null);

    // set image to ImageView
    mImage.setImageDrawable(d);

}

catch(IOException ex) {

      Log.e("I/O ERROR","Failed")
}
Zied R.
  • 4,909
  • 2
  • 36
  • 64
0

Instead of using the assets dir, put the file into /res/raw , and you can then access it using the following URI: android.resource://com.your.packagename/" + R.raw.sharethis

Adnan Abdollah Zaki
  • 3,938
  • 5
  • 48
  • 55