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

4 Answers 4

1

Try:

Bitmap shareThis = BitmapFactory.decodeResource(context.getResources(),
                                       R.drawable.shareThis);
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);
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")
}
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.