VOOZH about

URL: https://www.javacodegeeks.com/2012/02/android-read-file-from-assets.html

⇱ Android - Read file from Assets - Java Code Geeks


Description:

First of all, let me give you a link: AssetManager, through this class we can easily access any files lying inside the Assets directory of android application. (or any sub-folders inside the Assets directory).

Now, we can have an object of AssetManager class by using getAssets() method:

AssetManager assetManager = getAssets(); 

And the rest of the procedure i have given and described by making comments in the example so now go through the full solutions provided below with the output snap.

Output:

Solution:

ReadFileAssetsActivity.java

package com.paresh.readfileasset;

import java.io.IOException;
import java.io.InputStream;

import android.app.Activity;
import android.content.res.AssetManager;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * @author Paresh N. Mayani
 * @Website http://www.technotalkative.com
 */
public class ReadFileAssetsActivity extends Activity {

 /** Called when the activity is first created. */

 @Override
 public void onCreate(Bundle savedInstanceState) {

 super.onCreate(savedInstanceState);
 setContentView(R.layout.main);

 TextView txtContent = (TextView) findViewById(R.id.txtContent);
 TextView txtFileName = (TextView) findViewById(R.id.txtFileName);
 ImageView imgAssets = (ImageView) findViewById(R.id.imgAssets);

 AssetManager assetManager = getAssets();

 // To get names of all files inside the "Files" folder
 try {
 String[] files = assetManager.list("Files");

 for(int i=0; i<files.length; i++)="" {="" txtfilename.append("\n="" file="" :"+i+"="" name=""> "+files[i]);
 }
 } catch (IOException e1) {
 // TODO Auto-generated catch block
 e1.printStackTrace();
 }

 // To load text file
 InputStream input;
 try {
 input = assetManager.open("helloworld.txt");

 int size = input.available();
 byte[] buffer = new byte[size];
 input.read(buffer);
 input.close();

 // byte buffer into a string
 String text = new String(buffer);

 txtContent.setText(text);
 } catch (IOException e) {
 // TODO Auto-generated catch block
 e.printStackTrace();
 }

 // To load image
 try {
 // get input stream
 InputStream ims = assetManager.open("android_logo_small.jpg");

 // create drawable from stream
 Drawable d = Drawable.createFromStream(ims, null);

 // set the drawable to imageview
 imgAssets.setImageDrawable(d);
 }
 catch(IOException ex) {
 return;
 }
 }
}
</files.length;>

main.xml
Note: Please consider scrollview as ScrollView, textview as TextView….etc. Its just problem inside the code plugin.

<!--?xml version="1.0" encoding="utf-8"?-->

<scrollview xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent">

<linearlayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical">

 <textview android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" android:id="@+id/txtContent">

 <imageview android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/imgAssets">

 <textview android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/txtFileName">
</textview></imageview></textview></linearlayout>

</scrollview>

Download Full source code from here: Android – Read file from Assets

Reference: Android – Read file from Assets from our JCG partner Paresh N. Mayani at the TechnoTalkative blog.

Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy

Thank you!

We will contact you soon.

πŸ‘ Photo of Paresh Mayani
Paresh Mayani
February 22nd, 2012Last Updated: October 21st, 2012
21 835 1 minute read

Paresh Mayani

Paresh Mayani is a Mobile application developer from India, having been involved in Android app development since around 3 years. He writes technical articles at TechnoTalkative. Apart from his job, he manages Google Developer Group (GDG) - Ahmedabad and has been speaker in various events. He is very much active in supporting the Android developer community, from answering questions on StackOverflow to publishing articles with possible sample code. Currently he is holder of around 25000 reputation.
Subscribe

This site uses Akismet to reduce spam. Learn how your comment data is processed.

21 Comments
Oldest
Newest Most Voted
14 years ago

Thanx for sharing :)

0
Reply
rushank
12 years ago
Reply to  Paresh Mayani

hey can u tell me how can i open pdf file with pdfbox in android????? and without pdf viewer…plz
reply asap…….

0
Reply
Hanisha
10 years ago
Reply to  Paresh Mayani

can uh tell me how i use .docx file in android and open it??

0
Reply
13 years ago

i need to read jar file like you read text file and image how to do that?

0
Reply
13 years ago

Is Jar file readable?

0
Reply
ANKIT THAKUR
13 years ago

1) can we delete the asset folder file at run time programtically

0
Reply
rahul
11 years ago
Reply to  ANKIT THAKUR

no

0
Reply
12 years ago

Hi,i have my files ie images on assets folder.consider i have 3 images at first and when i launch app i could see all 3 images loaded.but say i updated it with 3 more images and relaunch app.i couldn’t find recently added images in that app.i use shared preference to save app state.what should i do in order to make other 3 images visible?

0
Reply
Akshay
12 years ago

any jar file is needed for this application? because , space character is displaying as question mark.

0
Reply
abhi
12 years ago

I want to read data from an image/picture and save that data in some storage like xml/text/world/Data Base. Image may contain any form/Drivers License or may be some Credit card.

Please advise.

0
Reply
Vani
12 years ago

Hi,

I want to open PDF file from assets folder, i tried lots of but its not open, we can open text file but i want to open pdf file, also i got my pdf file name when i do

String[] files = assetManager.list(β€œFiles”);
but its still not open
please help me how can i open pdf file in read mode.

1
Reply
Md Shoaib
12 years ago

I want to ask that how to attach the pdf file in my android app and send it.
And thanks in advance.

1
Reply
vineel
11 years ago
Reply to  Md Shoaib

search on google.

0
Reply
Hiren pithwa
11 years ago

Button click and open text file from asset folder…..how to make app

0
Reply
G Srinivasan
11 years ago

Hi

0
Reply
G Srinivasan
11 years ago

Super

0
Reply
versatile vasu
11 years ago

ur code looks for opening text files can u give codes for html file

1
Reply
vinit
11 years ago

Hi

I have xml files ijn asset folder and want to store them in encrypted form while runtime I have to decrype these file.
I know in java we can implement it using cipher package but in android how can I implement encryption and decryption logic.

0
Reply
sapna
10 years ago

how to move it in listview as u have moved data to textview

0
Reply
8 years ago

Thank You,That’s help a lot

0
Reply
8 years ago

how can i show,different data on different clickable list view. i store data in asset folder in form of xml file

0
Reply
Back to top button
Close
wpDiscuz