我开发了一个相册应用程序,我在其中使用了网格视图。该应用程序正在运行,但每次当我按下返回按钮并返回我的应用程序时,网格视图中都会出现一个图像副本。例如,我在网格视图中有 7 张图像,当我按下后退按钮然后返回应用程序时,它变为 14。下次总图像将是 21 等等。我正在从 SD 卡读取图像。我的应用程序具有全屏功能,所以同样的事情也会发生。我不明白为什么会这样。下面,我发布我的代码:
专辑活动
public class Album3Activity extends Activity {
static File [] mediaFiles;
static File imageDir;
static GridView gridView;
ImageAdapter adapter;
Intent in;
static String name = null;
static ArrayList<Bitmap> bmpArray = new ArrayList<Bitmap>();
static ArrayList<String> fileName = new ArrayList<String>();
public static final String TAG = "Album3Activity";
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.grid);
imageDir = new File(Environment.getExternalStorageDirectory().toString()+
"/diplomat");
mediaFiles = imageDir.listFiles();
//Log.d("Lenght of images",""+mediaFiles.length);
for(File file : mediaFiles)
{
bmpArray.add(convertToBitmap(file));
fileName.add(readFileName(file));
Log.d(TAG + "bmpArray Size", ""+bmpArray.size());
Log.d(TAG, "call to convertToBitmap");
}//for
adapter = new ImageAdapter(this, bmpArray, fileName);
gridView = (GridView)findViewById(R.id.gridview);
gridView.setAdapter(adapter);
gridView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3) {
in = new Intent(getApplicationContext(), FullScreen.class);
in.putExtra("id", position);
startActivity(in);
}
});
}//onCreate
public static Bitmap convertToBitmap(File file)
{
URL url = null;
try
{
url = file.toURL();
} catch (MalformedURLException e1)
{
Log.d(TAG, e1.toString());
}//catch
Bitmap bmp = null;
try
{
bmp = BitmapFactory.decodeStream(url.openStream());
//bmp.recycle();
}catch(Exception e)
{
Log.d(TAG, "Exception: "+e.toString());
}//catch
return bmp;
}//convertToBitmap
public String readFileName(File file){
String name = file.getName();
return name;
}//readFileName
}//class
图像适配器
public class ImageAdapter extends BaseAdapter {
Activity act;
ArrayList<Bitmap> mFiles;
static ArrayList<String> mName;
public static final String TAG = "ImageAdapter";
static String name = null;
public ImageAdapter(Activity act, ArrayList<Bitmap> mFiles, ArrayList<String> mName) {
super();
this.act = act;
this.mFiles = mFiles;
ImageAdapter.mName = mName;
Log.d("mfiles", "" + this.mFiles.size());
}// ImageAdapter
public int getCount() {
return mFiles.size();
}// getCount
public Object getItem(int postion) {
return mFiles.get(postion);
}// getItem
public long getItemId(int position) {
return 0;
}// getItemId
public static class ViewHolder {
ImageView iv;
TextView tv;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder view;
LayoutInflater li = act.getLayoutInflater();
if (convertView == null) {
Log.d("tag", "null");
view = new ViewHolder();
convertView = li.inflate(R.layout.gridview_row, null);
view.iv = (ImageView) convertView.findViewById(R.id.imageView1);
view.tv = (TextView) convertView.findViewById(R.id.textView1);
convertView.setTag(view);
}// if
else {
view = (ViewHolder) convertView.getTag();
}// else
view.iv.setImageBitmap(mFiles.get(position));
view.tv.setText(readName(position));
Log.d(TAG, "getView method called");
return convertView;
}// getView
public static String readName(int position) {
String name = mName.get(position);
return name;
}//readName
}// ImageAdapter
全屏.java
public class FullScreen extends Activity implements OnClickListener, OnGestureListener{
ImageView imageView1;
ViewSwitcher viewSwitcher;
ImageAdapter adapter = new ImageAdapter(getParent(), Album3Activity.bmpArray, Album3Activity.fileName);
Button avbryt, camera, trash, opendialog, slett,
avbryt1, epost, mms, avbryt2, facebook;
TextView textView;
ImageButton btnNext, btnPrevious;
GestureDetector gesturedetector = null;
Intent in;
int touches;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fullscreen);
viewSwitcher = (ViewSwitcher)findViewById(R.id.viewSwitcher);
imageView1 = (ImageView)findViewById(R.id.imageView1);
textView = (TextView)findViewById(R.id.textView1);
in = getIntent();
touches = in.getExtras().getInt("id");
imageView1.setImageBitmap((Bitmap)adapter.getItem(touches));
textView.setText(ImageAdapter.readName(touches));
btnNext = (ImageButton)findViewById(R.id.btnNext);
btnNext.setOnClickListener(this);
btnPrevious = (ImageButton)findViewById(R.id.btnPrevious);
btnPrevious.setOnClickListener(this);
trash = (Button)findViewById(R.id.trash);
trash.setOnClickListener(this);
opendialog = (Button)findViewById(R.id.opendialog);
opendialog.setOnClickListener(this);
avbryt = (Button)findViewById(R.id.avbryt);
avbryt.setOnClickListener(this);
camera = (Button)findViewById(R.id.camera_albumbutton);
camera.setOnClickListener(this);
gesturedetector=new GestureDetector(this, this);
}//onCreate
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnNext:
Animation animationNext = AnimationUtils.loadAnimation(this, R.anim.btn_style_next);
btnNext.startAnimation(animationNext);
nextView();
break;
case R.id.btnPrevious:
Animation animationPrevious = AnimationUtils.loadAnimation(this, R.anim.btn_style_previous);
btnPrevious.startAnimation(animationPrevious);
previousView();
break;
case R.id.trash:
final Dialog dialog = new Dialog(FullScreen.this);
dialog.setContentView(R.layout.trashdialog);
slett = (Button)dialog.findViewById(R.id.slett);
avbryt1 = (Button)dialog.findViewById(R.id.avbryt);
slett.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
File file = new File(Environment.getExternalStorageDirectory()+
"/diplomat/"+ ImageAdapter.readName(touches));
Log.d("FullScreen Path", file.getAbsolutePath());
Log.d("Hi1 touches value", ""+touches);
try
{
if(touches == (Album3Activity.bmpArray.size() - 1))
{
file.delete();
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED
, Uri.parse(Environment.getExternalStorageDirectory().toString()+"/diplomat")));
Album3Activity.bmpArray.remove(touches);
Log.d("Hi2 new bmpArray size", ""+Album3Activity.bmpArray.size());
Album3Activity.fileName.remove(touches);
Log.d("Hi2 new fileName size", ""+Album3Activity.fileName.size());
Album3Activity.gridView.invalidateViews();
adapter.notifyDataSetChanged();
touches = Album3Activity.bmpArray.size() - touches;
Log.d("FullScreen delete:if block", "Updated touches value:"+touches);
do
{
Log.d("FullScreen:delete if", "in while loop");
imageView1.setImageBitmap((Bitmap)adapter.getItem(touches));
textView.setText(ImageAdapter.readName(touches));
break;
}//do
while(Album3Activity.bmpArray.isEmpty() == false);
}//if
else
{
Log.d("fullscreen:delete else", "else block executed");
file.delete();
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED
, Uri.parse(Environment.getExternalStorageDirectory().toString()+"/diplomat")));
Album3Activity.bmpArray.remove(touches);
Log.d("Hi2 new bmpArray size", ""+Album3Activity.bmpArray.size());
Album3Activity.fileName.remove(touches);
Log.d("Hi2 new fileName size", ""+Album3Activity.fileName.size());
Album3Activity.gridView.invalidateViews();
adapter.notifyDataSetChanged();
imageView1.setImageBitmap((Bitmap)adapter.getItem(touches));
textView.setText(ImageAdapter.readName(touches));
}//else
}//try
catch(IndexOutOfBoundsException e)
{
Log.d("Fullscreen delete method", e.toString());
Toast.makeText(FullScreen.this, "No more image left to diaplay"
, Toast.LENGTH_SHORT).show();
}//catch
finally
{
dialog.dismiss();
}//finally
}//onClick
});
avbryt1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
dialog.cancel();
}//onClick
});
dialog.show();
break;
case R.id.opendialog:
final Dialog dialog1 = new Dialog(FullScreen.this);
dialog1.setContentView(R.layout.openbuttondialog);
epost = (Button)dialog1.findViewById(R.id.epost);
mms = (Button)dialog1.findViewById(R.id.mms);
facebook = (Button)dialog1.findViewById(R.id.facebook);
avbryt2 =(Button)dialog1.findViewById(R.id.openavbryt);
epost.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
}//onClick
});
mms.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
}//onClick
});
facebook.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
}//onClick
});
avbryt2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
dialog1.cancel();
}//onClick
});
dialog1.show();
break;
case R.id.avbryt:
break;
case R.id.camera_albumbutton:
break;
default:
break;
}//switch
}//onClick
private void previousView() {
if(touches > 0){
touches = touches - 1;
imageView1.setImageBitmap((Bitmap)adapter.getItem(touches));
textView.setText(ImageAdapter.readName(touches));
viewSwitcher.setInAnimation(this, R.anim.in_animation1);
viewSwitcher.setOutAnimation(this, R.anim.out_animation1);
viewSwitcher.showPrevious();
}//if
else{
Toast.makeText(this, "No image left", Toast.LENGTH_SHORT).show();
}//else
Log.d("Switcher", "previousView");
}//previousView
private void nextView() {
if(touches < Album3Activity.bmpArray.size() - 1){
touches = touches + 1;
imageView1.setImageBitmap((Bitmap)adapter.getItem(touches));
textView.setText(ImageAdapter.readName(touches));
viewSwitcher.setInAnimation(this, R.anim.in_animation);
viewSwitcher.setOutAnimation(this, R.anim.out_animation);
viewSwitcher.showNext();
}//if
else{
Toast.makeText(this, "No image left", Toast.LENGTH_SHORT).show();
}//else
Log.d("Switcher", "nextView");
}//nextView
public boolean onDown(MotionEvent e) {
// TODO Auto-generated method stub
return true;
}
int SWIPE_MIN_VELOCITY = 50;
int SWIPE_MIN_DISTANCE = 50;
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
float ev1X = e1.getX();
float ev2X = e2.getX();
Log.d("On", "Onfling");
//Get distance of X (e1) to X (e2)
final float xdistance = Math.abs(ev1X - ev2X);
//Get veclocity of cusor
//Váºn tốc = số Ä‘iểm ảnh (px) / giây
final float xvelocity = Math.abs(velocityX);
//Váºn tốc chuyển đổi X > 100 và khoảng cách từ Ä‘iểm kéo đầu đến Ä‘iểm kéo cuối > 100
if( (xvelocity > SWIPE_MIN_VELOCITY) && (xdistance > SWIPE_MIN_DISTANCE) )
{
if(ev1X > ev2X)//Switch Left
{
previousView();
}
else//Switch Right
{
nextView();
}
}
return false;
}
public void onLongPress(MotionEvent e) {
// TODO Auto-generated method stub
}
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
float distanceY) {
// TODO Auto-generated method stub
return false;
}
public void onShowPress(MotionEvent e) {
// TODO Auto-generated method stub
}
public boolean onSingleTapUp(MotionEvent e) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
Log.d("on touch","touch");
// TODO Auto-generated method stub
//return gesturedetector.onTouchEvent(event);
this.gesturedetector.onTouchEvent(event);
return super.onTouchEvent(event);
}
}//FullScreen