I want to show dialog file chooser but it is showing deprecated onCreateDialog in FileDialog Class and not called. As newbie, i want to know how to solve it.
Inside MainActivity OnCreate
ImageView imageFileView = (ImageView) findViewById(R.id.imageFileView);
imageFileView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
openFile();
}
});
private void openFile() {
FileDialog fileDialog = new FileDialog();
fileDialog.loadFileList();
//fileDialog.onCreateDialog(10);
}
FileDialog Class
public class FileDialog extends Activity {
private String[] mFileList;
private File mPath = new File(Environment.getExternalStorageDirectory() + "//test//");
private String mChosenFile;
private static final String FTYPE = ".txt";
private static final int DIALOG_LOAD_FILE = 1000;
public void loadFileList() {
try {
mPath.mkdirs();
} catch (SecurityException e) {
Log.e("123", "unable to write on the sd card " + e.toString());
}
if (mPath.exists()) {
FilenameFilter filter = new FilenameFilter() {
@Override
public boolean accept(File dir, String filename) {
File sel = new File(dir, filename);
return filename.contains(FTYPE) || sel.isDirectory();
}
};
//mFileList = new String[]{"a.txt","b.txt"};
mFileList = mPath.list(filter);
} else {
//mFileList = new String[]{"a.txt","b.txt"};
mFileList = new String[0];
}
Log.d("abc", "" + mFileList.length);
//showDialog(DIALOG_LOAD_FILE);
}
@Override
protected Dialog onCreateDialog(int id) {
Dialog dialog = null;
AlertDialog.Builder builder= new AlertDialog.Builder(this);
Log.d("abc", "58695896");
switch (id) {
case DIALOG_LOAD_FILE:
builder.setTitle("Choose your file");
if (mFileList == null) {
Log.e("abc", "Showing file picker before loading the file list");
dialog = builder.create();
return dialog;
}
builder.setItems(mFileList, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
mChosenFile = mFileList[which];
//you can do stuff with the file here too
}
});
break;
}
dialog = builder.show();
return dialog;
}
}
0 comments:
Post a Comment