Skip to content

Instantly share code, notes, and snippets.

@dasgoll
Created May 6, 2014 19:56
Show Gist options
  • Save dasgoll/b6c1326d674838e9ded2 to your computer and use it in GitHub Desktop.
Save dasgoll/b6c1326d674838e9ded2 to your computer and use it in GitHub Desktop.
// and have three files under assets called f1.txt f2.txt f3.txt
package aexp.assets;
import android.app.Activity;
import android.content.res.AssetManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import java.io.IOException;
import java.io.InputStream;
public class Assets extends Activity
{
public static final String LOG_TAG = "Assets";
static final int views[] = {
R.id.f1,
R.id.f2,
R.id.f3
};
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
AssetManager am = getResources().getAssets();
String assets[] = null;
try {
assets = am.list( "" );
for( int i = 0 ; i < views.length ; ++i ) {
if( i >= assets.length )
break;
TextView t = (TextView)findViewById( views[i] );
readTextResource( am, t, assets[i] );
}
} catch( IOException ex ) {
Log.e( LOG_TAG,
"I/O Exception",
ex );
}
}
private void readTextResource( AssetManager am, TextView t,String asset )
throws IOException {
InputStream is = am.open( asset );
StringBuffer sb = new StringBuffer();
while( true ) {
int c = is.read();
if( c < 0 )
break;
if( c >= 32 )
sb.append( (char)c );
}
t.setText( new String( sb ) );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment