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
  • 4,328
  • 6
  • 52
  • 58
suraj mahajan
  • 832
  • 1
  • 12
  • 21

4 Answers4

1

Try:

Bitmap shareThis = BitmapFactory.decodeResource(context.getResources(),
                                       R.drawable.shareThis);
Karan
  • 2,120
  • 15
  • 27
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
  • 250
  • 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,964
  • 2
  • 36
  • 67
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
  • 4,328
  • 6
  • 52
  • 58