Pages

Friday 23 August 2013

READ THE FILE AND IMAGE FROM ASSERT FOLDER   :

In This code you read the file and Image from Assert folder 

Main.Class File:


public void onCreate(Bundle savedInstanceState) {

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


TextView readtext = (TextView) findViewById(R.id.readtext);
ImageView image = (ImageView) findViewById(R.id.image);

AssetManager assetManager = getAssets();
InputStream input;
        
     // To read the text file from assert
try {
input = assetManager.open("helloworld.txt");

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

        String text = new String(buffer);

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

// To get the image from assert
    try {
    InputStream ims = assetManager.open("android.jpg");
    Drawable d = Drawable.createFromStream(ims, null);
    image.setImageDrawable(d);
    }
    catch(IOException ex) {
    return;
    }
}

}


Main.xml:

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

        <ImageView
            android:id="@+id/image"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />

        <TextView
            android:id="@+id/readtext"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>


Screen Short :