22182

Android: FileNotFoundException on an image in drawable

Question:

I'm trying to use this code that I've seen on multiple posts throughout stackoverflow

public static Bitmap loadBitmap(Context context, String filename) throws IOException { AssetManager assets = context.getResources().getAssets(); InputStream buf = new BufferedInputStream((assets.open("drawable/" + filename))); Bitmap bitmap = BitmapFactory.decodeStream(buf); return bitmap; }

but I'm getting a "FileNotFoundException: drawable/filename". I definitely have a file with the name I'm trying to load in the drawable folders. Any ideas? I've tried with and without the file extension, tried putting the file into every folder and tried multiple phones/emulator.

Answer1:

The AssetManager is intended to access the data in the assets folder. So in your example, it looks for assets/drawable/filename and does not find anything.

To get a resource from drawable, one should use

Drawable d = getResources().getDrawable(R.drawable.filename);

If you're sure it's a bitmap, you can do it like so:

Bitmap bm = ((BitmapDrawable)getResources() .getDrawable(R.drawable.filename)).getBitmap();

Answer2:

You can do it like this...

// load image from Assets folder // get input stream InputStream mIms = getAssets().open("ic_launcher.png"); // load image as Drawable Drawable mDraw = Drawable.createFromStream(mIms, null); // set image to ImageView mIms.setImageDrawable(mDraw);

Recommend

  • Avoid garbage collection when using sockets
  • Faster way than Scanner or BufferedReader reading multiline data from STDIN?
  • How to make the standard music player find my files
  • Why doesn't UriInfo.getQueryParameters() decode '+'?
  • Unable to create folder inside Android/data
  • android downloading multiple files with InputStream FileOutputStream
  • how to use font file
  • How to get a path to resources?
  • Colour resource ID returning wrong value
  • problem with code to combine two images in android?
  • Andengine loading graphics: why is my background texture small and upside down
  • Declare struct containing time_t field in Python CFFI
  • getResource() to a file on runtime
  • Android ripple for button not working
  • GCM registration works for debug APK but not release APK
  • Android how to set a transparent image resource of a view? or Remove the image?
  • Change Design Support Navigation View Header Title Programmatically
  • angularjs ng-bind-html html input part missing
  • Rails 4.0.0 jQuery Mobile button icons not showing in production
  • APNs messages are delivered but not received on iOS device
  • PHP socket_read() only gets the first byte of the stream
  • Simple test app deploys to Heroku but won't run
  • WSO2 ESB 4.0.3 - Error installing Data Services feature from 4.0.* repositories
  • Font Awesome in Viewpager Previous Next
  • java.net.SocketTimeoutException on embedded tomcat with jersey
  • write text on image and show it to a imageview
  • insert a picture into database(sqlite) with java code. what should i do?
  • Unable to resolve Static method?
  • android Navigation Bar hiding and persantage of usable screen overlap
  • In Java, how can I construct a File from a resource?
  • Read text file that is not in the main package in a runnable jar
  • copying resource to sdcard gives a damaged file in android
  • Bad request using file_get_contents for PUT request in PHP
  • Xamarin Forms - UWP Fonts
  • HTML download movie download link
  • How to redirect a user to a different server and include HTTP basic authentication credentials?
  • Can I make an Android app that runs a web view in Chrome 39?
  • Arrow is showed instead of the material design version hamburger icon. Why doesn't syncState in
  • LevelDB C iterator
  • Linking SubReports Without LinkChild/LinkMaster