package mike.android.sdcard;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import mike.android.R;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnDismissListener;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class BrowseSDCardImagesActivity extends Activity {
private ImageView imageView;
private Button previousButton;
private Button nextButton;
private TextView titleTextView;
public ProgressDialog progressDialog = null;
private int imageIndex = -1;
private static final CharSequence dialogTitle = "载入图片中";
private static final CharSequence dialogBody = "载入图片中";
private List<String> imagePaths;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final List<String> imagePaths = getSDCard();
if (imagePaths.size() > 0) {
Toast.makeText(this, "有" + imagePaths.size() + "张相片在SD卡根目录", Toast.LENGTH_LONG).show();
imageView = (ImageView) findViewById(R.id.imageView1);
titleTextView = (TextView) findViewById(R.id.titleTextView);
previousButton = (Button) findViewById(R.id.previousButton);
nextButton = (Button) findViewById(R.id.nextButton);
nextButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
switchToNextImage();
}
});
previousButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
switchToPreviousImage();
}
});
} else {
Toast.makeText(this, "无相片在SD卡根目录", Toast.LENGTH_LONG).show();
}
}
public String getSDPath() {
File sdDir = null;
boolean sdCardExist = Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED); // 判断sd卡是否存在
if (sdCardExist) {
sdDir = Environment.getExternalStorageDirectory();
}
return sdDir != null ? sdDir.toString() : "没发现SDCard";
}
private List<String> getSDCard() {
imagePaths = new ArrayList<String>();
try {
File file = new File(getSDPath());
File[] files = file.listFiles();
for (File theFile : files) {
if (matchImageFile(theFile.getPath())) {
imagePaths.add(theFile.getPath());
}
}
} catch (Exception e) {
return imagePaths;
}
return imagePaths;
}
private boolean matchImageFile(String fileName) {
String extension = fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length()).toLowerCase();
if (extension.equals("jpg") || extension.equals("png") || extension.equals("gif") || extension.equals("jpeg") || extension.equals("bmp")) {
return true;
}
return false;
}
private String parsePathForImageName(String path) {
int lastSeparatorIndex = path.lastIndexOf("/");
return path.substring(lastSeparatorIndex + 1, path.length());
}
private void switchToNextImage() {
next();
showProgressDialog();
}
private void switchToPreviousImage() {
previous();
showProgressDialog();
}
private void showProgressDialog() {
progressDialog = ProgressDialog.show(BrowseSDCardImagesActivity.this, dialogTitle, dialogBody, true);
ProcessThread processThread = new ProcessThread();
progressDialog.setOnDismissListener(processThread);
progressDialog.show();
processThread.start();
}
private void next() {
if (imageIndex < imagePaths.size() - 1) {
imageIndex += 1;
} else {
imageIndex = 0;
}
}
private void previous() {
if (imageIndex > 1) {
imageIndex -= 1;
} else {
imageIndex = imagePaths.size() - 1;
}
}
private void drawImage() {
String path = imagePaths.get(imageIndex);
titleTextView.setText("第" + (imageIndex + 1) + "张相片:" + parsePathForImageName(path));
Bitmap firstBitmap = BitmapFactory.decodeFile(path);
imageView.setImageBitmap(firstBitmap);
}
private class ProcessThread extends Thread implements OnDismissListener {
public void run() {
try {
sleep(200);
} catch (InterruptedException e) {
} finally {
progressDialog.dismiss();
}
}
@Override
public void onDismiss(DialogInterface arg0) {
drawImage();
}
}
}
- 1
- 2
- 3
- 4
- 5
前往页