Custom dialog was implemented to load individual image(jpg). In this article, the custom dialog is used to open folder, instead of individual file. After dismissed, the images(jpg) in the selected folder will be listed.
dialoglayout.xml, layout of the custom dislog.
dialoglayout.xml, layout of the custom dislog.
AndroidGalleryActivity.java, the main code.
package com.AndroidGallery;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.app.Dialog;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.MimeTypeMap;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class AndroidGalleryActivity extends Activity {
Button buttonOpenDialog;
Button buttonUp, buttonSelectFolder;
TextView jpgList;
String KEY_TEXTPSS = "TEXTPSS";
static final int CUSTOM_DIALOG_ID = 0;
ListView dialog_ListView;
File root;
File curFolder;
private ListfileList = new ArrayList ();
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
jpgList = (TextView)findViewById(R.id.jpglist);
buttonOpenDialog = (Button)findViewById(R.id.opendialog);
buttonOpenDialog.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View arg0) {
showDialog(CUSTOM_DIALOG_ID);
}});
root = new File(Environment
.getExternalStorageDirectory()
.getAbsolutePath());
curFolder = root;
ListJpgInFolder(curFolder);
}
@Override
protected Dialog onCreateDialog(int id) {
Dialog dialog = null;
switch(id) {
case CUSTOM_DIALOG_ID:
dialog = new Dialog(AndroidGalleryActivity.this);
dialog.setContentView(R.layout.dialoglayout);
dialog.setTitle("Select JPG");
dialog.setCancelable(true);
dialog.setCanceledOnTouchOutside(true);
buttonUp = (Button)dialog.findViewById(R.id.up);
buttonUp.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
ListDir(curFolder.getParentFile());
}});
buttonSelectFolder = (Button)dialog.findViewById(R.id.selectfolder);
buttonSelectFolder.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(AndroidGalleryActivity.this,
curFolder + " selected",
Toast.LENGTH_LONG).show();
dismissDialog(CUSTOM_DIALOG_ID);
ListJpgInFolder(curFolder);
}});
//Prepare ListView in dialog
dialog_ListView = (ListView)dialog.findViewById(R.id.dialoglist);
dialog_ListView.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView parent, View view,
int position, long id) {
File selected = new File(fileList.get(position));
if(selected.isDirectory()){
ListDir(selected);
}else {
/*
Toast.makeText(AndroidGalleryActivity.this,
selected.toString() + " selected",
Toast.LENGTH_LONG).show();
dismissDialog(CUSTOM_DIALOG_ID);
Bitmap bm = BitmapFactory.decodeFile(selected.getAbsolutePath());
image.setImageBitmap(bm);
*/
}
}});
break;
}
return dialog;
}
@Override
protected void onPrepareDialog(int id, Dialog dialog, Bundle bundle) {
// TODO Auto-generated method stub
super.onPrepareDialog(id, dialog, bundle);
switch(id) {
case CUSTOM_DIALOG_ID:
ListDir(curFolder);
break;
}
}
void ListDir(File f){
if(f.equals(root)){
buttonUp.setEnabled(false);
}else{
buttonUp.setEnabled(true);
}
curFolder = f;
buttonSelectFolder.setText("Select Folder " + curFolder);
File[] files = f.listFiles();
fileList.clear();
for (File file : files){
if(file.isDirectory()){
fileList.add(file.getPath());
}else{
Uri selectedUri = Uri.fromFile(file);
String fileExtension
= MimeTypeMap.getFileExtensionFromUrl(selectedUri.toString());
if(fileExtension.equalsIgnoreCase("jpg")){
fileList.add(file.getName());
}
}
}
ArrayAdapterdirectoryList
= new ArrayAdapter(this,
android.R.layout.simple_list_item_1, fileList);
dialog_ListView.setAdapter(directoryList);
}
private void ListJpgInFolder(File folder){
String jpgs = "JPG fiels in folder " + folder.getAbsolutePath() + "\n\n";
File[] files = folder.listFiles();
for (File file : files){
if(!file.isDirectory()){
Uri selectedUri = Uri.fromFile(file);
String fileExtension
= MimeTypeMap.getFileExtensionFromUrl(selectedUri.toString());
if(fileExtension.equalsIgnoreCase("jpg")){
jpgs += file.getAbsolutePath() + "\n";
}
}
}
jpgList.setText(jpgs);
}
}
main.xml, the main layout.
Update Contact :
No Wa/Telepon (puat) : 085267792168
No Wa/Telepon (fajar) : 085369237896
Email : Fajarudinsidik@gmail.com
No Wa/Telepon (puat) : 085267792168
No Wa/Telepon (fajar) : 085369237896
Email: Fajarudinsidik@gmail.com
atau Kirimkan Private messanger melalui email dengan klik tombol order dibawah ini :