简单图片浏览

public class Main extends Activity implements OnClickListener {


private ImageView imageView;
private Button preButton;
private Button nextButton;
private TextView title_tv;
public ProgressDialog progressDialog = null;


private int imageIndex = -1;
Bitmap bitmap = null;
private List<String> imagePaths;// 瀛樻斁鍥剧墖璺緞


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// VMRuntime.getRuntime().setMinimumHeapSize(10 * 1024 * 1024); //
// 璁剧疆鏈??灏廻eap鍐呭瓨涓??MB澶у皬銆??
// VMRuntime.getRuntime().setTargetHeapUtilization(0.75f);


imagePaths = getSDCard();// 鑾峰彇鍥剧墖鐨勮矾寰??
for (int i = 0; i < imagePaths.size(); i++) {
System.out.println(imagePaths.get(i));
}

getView();

if (imagePaths.size() > 0) {
preButton.setOnClickListener(this);
nextButton.setOnClickListener(this);
imageIndex = 0;
try {
title_tv.setText("绗????" + getName(imagePaths.get(0)));
bitmap = BitmapFactory.decodeFile(imagePaths.get(0));
imageView.setImageBitmap(bitmap);


} catch (Exception e) {
// TODO: handle exception
}


} else {
Toast.makeText(this, "鏃犵収鐗??", 2000).show();
}


}

@Override
protected void onDestroy() {
// TODO Auto-generated method stub
recycleBitMap(bitmap);
super.onDestroy();
}


@Override
protected void onPause() {
// TODO Auto-generated method stub
recycleBitMap(bitmap);
super.onPause();
}


@Override
protected void onRestart() {
// TODO Auto-generated method stub
recycleBitMap(bitmap);
super.onRestart();
}


@Override
protected void onResume() {
// TODO Auto-generated method stub
recycleBitMap(bitmap);
super.onResume();
}


// 缁樺埗鍥剧墖
private void drawImage() {
try {
String path = imagePaths.get(imageIndex);
title_tv.setText("绗??" + (imageIndex + 1) + "寮??" + getName(path));
bitmap = BitmapFactory.decodeFile(path);
imageView.setImageBitmap(bitmap);


} catch (Exception e) {
// TODO: handle exception
}


} // 涓嬩竴寮??
private void ToNextImage() {
if (imageIndex < imagePaths.size() - 1) {
imageIndex += 1;
} else {
imageIndex = 0;
}
// System.out.println(imageIndex);
showProgressDialog();
}


// 涓婁竴寮??
private void ToPreviousImage() {
if (imageIndex > 0) {
imageIndex -= 1;
} else {
imageIndex = imagePaths.size() - 1;
}
// System.out.println(imageIndex);
showProgressDialog();
} // 鑾峰緱SD鐨勮矾寰??
public String getSDPath() {
File sdPath = null;
if (Environment.getExternalStorageState().equals(
android.os.Environment.MEDIA_MOUNTED)) { // sd鍗″瓨鍦??
sdPath = new File(Environment.getExternalStorageDirectory()
.getAbsolutePath()
+ File.separator + "mobileoa" + File.separator + "photo");// 鑾峰緱鏍硅矾寰??
}
return sdPath != null ? sdPath.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 (isImageFile(theFile.getPath())) {
imagePaths.add(theFile.getPath());
}
}
} catch (Exception e) {
return imagePaths;
}


return imagePaths;
}// 鍒ゆ柇鏄惁鏄浘鐗囨枃浠??
private boolean isImageFile(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 getName(String path) {
int lastSeparatorIndex = path.lastIndexOf("/");
return path.substring(lastSeparatorIndex + 1, path.length());
}


// 鏄剧ず杩涘害鏉??
private void showProgressDialog() {
progressDialog = ProgressDialog.show(Main.this, "杞藉叆鍥剧墖涓??...", "杞藉叆鍥剧墖涓??...",
true);
ProcessThread processThread = new ProcessThread();
progressDialog.setOnDismissListener(processThread);
progressDialog.show();
processThread.start();
} // 灏嗙粯鍒跺浘鐗囨斁鍒颁竴涓嚎绋嬪綋涓??
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();// 缁樺埗鍥剧墖
}
}


public void getView() {
imageView = (ImageView) findViewById(R.id.imageView1);
title_tv = (TextView) findViewById(R.id.titleTextView);
preButton = (Button) findViewById(R.id.previousButton);
nextButton = (Button) findViewById(R.id.nextButton);
}

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.previousButton:


ToPreviousImage();
break;
case R.id.nextButton:
ToNextImage();
break;
default:
break;
}
}


// 閲婃斁bitmap鎵??鍗犵敤鐨勫唴瀛??
public void recycleBitMap(Bitmap bitmap) {
if (bitmap != null || bitmap.isRecycled() == false) {
bitmap.recycle();
}
}


}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值