Opening a File from assets folder in android
These Lines are working perfectly--
InputStream assetInStream=null;
try {
assetInStream=getAssets().open("icon.png");
Bitmap bit=BitmapFactory.decodeStream(assetInStream);
img.setImageBitmap(bit);
} catch (IOException e) {
e.printStackTrace();
} finally {
if(assetInStream!=null)
assetInStream.close();
}
If your image is very big then you should scale your image before decoding it into Bitmap. See How to display large image efficiently
How to access a file from asset/raw directory
Place your text file in the /assets directory under the Android project and use AssetManager class as follows to access it.
AssetManager am = context.getAssets();
InputStream is = am.open("default_book.txt");
Or you can also put the file in the /res/raw directory, from where the file can be accessed by an id as follows
InputStream is =
context.getResources().openRawResource(R.raw.default_book);
read file from assets
Here is what I do in an activity for buffered reading extend/modify to match your needs
BufferedReader reader = null;
try {
reader = new BufferedReader(
new InputStreamReader(getAssets().open("filename.txt")));
// do reading, usually loop until end of file reading
String mLine;
while ((mLine = reader.readLine()) != null) {
//process line
...
}
} catch (IOException e) {
//log the exception
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
//log the exception
}
}
}
EDIT : My answer is perhaps useless if your question is on how to do it outside of an activity. If your question is simply how to read a file from asset then the answer is above.
UPDATE :
To open a file specifying the type simply add the type in the InputStreamReader call as follow.
BufferedReader reader = null;
try {
reader = new BufferedReader(
new InputStreamReader(getAssets().open("filename.txt"), "UTF-8"));
// do reading, usually loop until end of file reading
String mLine;
while ((mLine = reader.readLine()) != null) {
//process line
...
}
} catch (IOException e) {
//log the exception
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
//log the exception
}
}
}
EDIT
As @Stan says in the comment, the code I am giving is not summing up lines. mLine
is replaced every pass. That's why I wrote //process line
. I assume the file contains some sort of data (i.e a contact list) and each line should be processed separately.
In case you simply want to load the file without any kind of processing you will have to sum up mLine
at each pass using StringBuilder()
and appending each pass.
ANOTHER EDIT
According to the comment of @Vincent I added the finally
block.
Also note that in Java 7 and upper you can use try-with-resources
to use the AutoCloseable
and Closeable
features of recent Java.
CONTEXT
In a comment @LunarWatcher points out that getAssets()
is a class
in context
. So, if you call it outside of an activity
you need to refer to it and pass the context instance to the activity.
ContextInstance.getAssets();
This is explained in the answer of @Maneesh. So if this is useful to you upvote his answer because that's him who pointed that out.
open a file outside of assets folder by assetManager
finally, I soloved it by using getClass().getResourceAsStream() instead of assetManager:
public byte[] loadBinAsset(String name) {
AssetManager assets = context.getResources().getAssets();
InputStream stream = null;
try {
try {
stream = assets.open(name);
} catch (IOException e) {
stream = context.getClass().getResourceAsStream("/" + name);
}
return readFully(stream);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (stream != null) {
stream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
How to open and read a text file in my /assets directory?
What you might want to try is use:
AssetManager am = getAssets();
InputStream is;
is = am.open(pathToFile.txt);
Then use a byte[] buffer (get size using is.available(); ) and is.read(buffer)
Don't forget to is.close() after read.
If you're not in an Activity you have to use context.getAssets() in order to get the AssetManager.
Hope that helps you.
Related Topics
Disable The Swipe Gesture That Opens The Navigation Drawer in Android
Using Android Google Maps V2 with Custom View or Imageview
Firebase Servervalue.Timestamp in Java Data Models Objects
How to Bring a Background Task to The Front Below Honeycomb
Security "Crypto" Provider Deprecated in Android N
How to Embed a Youtube Clip in a Webview on Android
Play-Services-Ads Conflicts with Appcompat
How to Handle Asynctask Onpostexecute When Paused to Avoid Illegalstateexception
App Crash After Activity Has Been Killed in Background
Fcm (Firebase Cloud Messaging) How to Send to All Phones
Read Logs from All Apps on Android from Within an App for Android 4.2+
Recyclerview with Multiple Countdown Timers Causes Flickering
How to Pass Image Data from One Activity to Another Activity
How to Reduce Latency in Mediacodec Video/Avc Decoding
Android/Java: How to Delay Return in a Method
Disabling Android's Chrome Pull-Down-To-Refresh Feature
Google Cloud Messaging (Gcm) with Local Device Groups on Android Gives Http Error Code 401