Get assets folder files to array of strings Android example
Get assets folder files to array of strings.
Its show files in assets folder and sub folders:
Its show files in assets folder and sub folders:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final AssetManager assetManager = getAssets();
try {
// for assets folder add empty string
String[] filelist = assetManager.list("");
// for assets/subFolderInAssets add only subfolder name
String[] filelistInSubfolder = assetManager.list("subFolderInAssets");
if (filelist == null) {
// dir does not exist or is not a directory
} else {
for (int i=0; i<filelist.length; i++) {
// Get filename of file or directory
String filename = filelist[i];
}
}
// if(filelistInSubfolder == null) ............
} catch (IOException e) {
e.printStackTrace();
}
}
396LW NO topic_id
AD
Další témata ....(Topics)
Bitmap Width, Height without memory allocation:
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(getResources(), R.id.myimage, options);
// now opts.outWidth and opts.outHeight are the dimension of the
// bitmap, even though Bitmap is null
int imageHeight = options.outHeight;
int imageWidth = options.outWidth;
String imageType = options.outMimeType;
private int _nVersionCode = 0;
private boolean _bNewVersion = false;
// onCreate
_nVersionCode = this.getPackageManager()
.getPackageInfo(this.getPackageName(), 0).versionCode;
Log.d(String.valueOf(_nVersionCode), "versionCode");
// onStart loadPreferences
public void loadPreferences() {
//SharedPreferences settings = getSharedPreferences(F.PREFERENCES_NAME, 0);
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
int nOldVersionCode = settings.getInt("_nVersionCode", 0);// old vesion
if(_nVersionCode > nOldVersionCode)
_bNewVersion = true;
}
// onDestroy savePreferences
public void savePreferences() {
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = settings.edit();
editor.putInt("_nVersionCode", _nVersionCode); // save current version code
editor.commit();
}
If you using Context as parameter of function try this solution:
public class MyActivity extends Activity {
// bla bla bla .......
//error
myFc( getapplicationcontext());
// OK
myFc(MyActivity.this);
ScrollTo(), getTop(), getBottom(), getLeft(), getRight(), ScrollView, LinearLayout Android Java xml example.
How get position of a View.
MainClass.java
main.xml
How get position of a View.
MainClass.java
/*
public void scrollTo (int x, int y)
Since: API Level 1
Set the scrolled position of your view. This will cause a call to onScrollChanged(int, int, int, int) and the view will be invalidated.
This version also clamps the scrolling to the bounds of our child.
Parameters
x the x position to scroll to
y the y position to scroll to
*/
// sroll to top of hscrollViewMain
ScrollView hscrollViewMain = (ScrollView)findViewById(R.id.scrollViewMain);
hscrollViewMain.scrollTo(0, 0); // scroll to application top
// get position of a View
EditText hEdit = (EditText)findViewById(R.id.username_edit);
int nY_Pos = hEdit.getTop(); // getBottom(); X_pos getLeft(); getRight();
// scroll to top of hEdit
hscrollViewMain.scrollTo(0,nY_Pos);
main.xml
<LinearLayout
xmlns:android="//schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ScrollView
android:id="@*id/scrollViewMain"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical"
android:paddingTop="5dip"
android:paddingBottom="13dip"
android:paddingLeft="20dip"
android:paddingRight="20dip">
<TextView
android:id="@+id/message"
android:textAppearance="?android:attr/textAppearanceSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dip" />
<TextView
android:textAppearance="?android:attr/textAppearanceSmall"
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/login_activity_username_label" />
<EditText
android:id="@+id/username_edit"
android:singleLine="true"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:minWidth="250dip"
android:scrollHorizontally="true"
android:capitalize="none"
android:autoText="false"
android:inputType="textEmailAddress" />
</LinearLayout>
</ScrollView>
</LinearLayout>
Canvas, drawLine(), setStrokeWidth(), Paint, setAntiAlias(boolean), onDraw()
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new SampleView(this));
}
private static class SampleView extends View {
// CONSTRUCTOR
public SampleView(Context context) {
super(context);
setFocusable(true);
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawColor(Color.YELLOW);
Paint p = new Paint();
// smooths
p.setAntiAlias(true);
p.setColor(Color.RED);
p.setStrokeWidth(4.5f);
// opacity
p.setAlpha(0x80); //
// drawLine (float startX, float startY, float stopX, float stopY,
// Paint paint)
canvas.drawLine(0, 0, 40, 40, p);
canvas.drawLine(40, 0, 0, 40, p);
}
}
}
Editace: 2011-09-24 12:18:23
Počet článků v kategorii: 396
Url:get-assets-folder-files-to-array-of-strings-android-example