每当我从ViewPager中删除我的图像时。然后有时下一个图像将被删除。或者有时候没有图像删除。
我错了,我不知道。
我的 GallaryActivity
public class GalleryActivity extends AppCompatActivity implements View.OnClickListener {
ImageButton imgBtnDelete;
TextView tvImageCount;
ViewPager galleryViewPager;
GalleryAdapter adapter;
private static String filePath = "";
public int mPosition = 0;
// File Code
public File mDirectory;
File[] imageList;
ArrayList<Integer> mTotalImages = new ArrayList<>();
ArrayList<String> mFilePaths = new ArrayList<>();
Uri[] mUrls;
String[] mFiles, mFileNames;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Hide the Status bar...Write Before setContentView()
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
setContentView(R.layout.activity_gallery);
imgBtnDelete = (ImageButton) findViewById(R.id.imgBtnDelete);
imgBtnDelete.setOnClickListener(this);
// File Code
mDirectory = new File(android.os.Environment.getExternalStorageDirectory(), "CameraApp/Images");
imageList = mDirectory.listFiles(new FilenameFilter() {
@Override
public boolean accept(File file, String name) {
return ((name.endsWith(".jpg")) || (name.endsWith(".png")) || (name.endsWith(".mp4")));
}
});
int imgLength = (imageList == null) ? 0 : imageList.length;
if (imgLength > 0) {
mFiles = new String[imgLength];
// add imgLength here for set Total Images in TextView
for (int k = 0; k < imageList.length; k++) {
mTotalImages.add(imgLength);
filePath = imageList[k].getAbsolutePath();
mFilePaths.add(filePath);
}
mFileNames = new String[imgLength];
tvImageCount.setVisibility(View.VISIBLE);
tvImageCount.setText(1 + "/" + mTotalImages.size());
mUrls = new Uri[imgLength];
int i, j = 0;
for (i = imgLength - 1; i >= 0; i--) {
mFiles[j] = imageList[i].getAbsolutePath();
mFileNames[j] = imageList[i].getName();
j++;
}
galleryViewPager = (ViewPager) findViewById(R.id.galleryViewPager);
adapter = new GalleryAdapter(this, mFiles);
galleryViewPager.setAdapter(adapter);
galleryViewPager.setOnPageChangeListener(pageChangeListener);
} else {
tvImageCount.setVisibility(View.INVISIBLE);
Toast.makeText(this, "Please capture some images from App!", Toast.LENGTH_SHORT).show();
}
}
OnPageChangeListener pageChangeListener = new OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
mPosition = position;
tvImageCount.setText((position + 1) + "/" + mTotalImages.size());
Toast.makeText(GalleryActivity.this, "mPosition is : " + mPosition, Toast.LENGTH_SHORT).show();
}
@Override
public void onPageScrollStateChanged(int state) {
}
};
private void openAlert() {
new AlertDialog.Builder(this)
.setTitle("Delete Image")
.setMessage("Are you sure you want to delete this Image?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// continue with delete
DeleteImage();
adapter.notifyDataSetChanged();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// do nothing
dialog.dismiss();
}
})
.setIcon(android.R.drawable.ic_dialog_alert)
.setCancelable(true)
.show();
}
我的 DeleteImage()
private void DeleteImage() {
File file = new File(mFilePaths.get(mPosition));
boolean deleted = file.delete();
if (deleted) {
mFilePaths.remove(mPosition);
if (mFilePaths.size() == 0) {
tvImageCount.setVisibility(View.INVISIBLE);
}
if (mFilePaths.size() != 0 && mFilePaths.size() != 1) {
adapter.notifyDataSetChanged();
galleryViewPager.setCurrentItem(mPosition + 1);
}
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
Uri contentUri = Uri.fromFile(file);
mediaScanIntent.setData(contentUri);
this.sendBroadcast(mediaScanIntent);
} else {
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"
+ Environment.getExternalStorageDirectory())));
}
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.imgBtnDelete:
if (mDirectory.exists() && mDirectory != null && mFilePaths.size() != 0) {
openAlert();
} else {
Toast.makeText(this, "No Images to Delete!", Toast.LENGTH_SHORT).show();
}
break;
default:
break;
}
}
以下是我的 GallaryAdapter 的Viewpager
public class GalleryAdapter extends PagerAdapter {
private Context mContext;
private String[] mFiles;
public GalleryAdapter(Context mContext, String[] mFiles) {
this.mContext = mContext;
this.mFiles = mFiles;
}
@Override
public int getCount() {
return mFiles.length;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
LayoutInflater mLayoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = mLayoutInflater.inflate(R.layout.gallary_viewpager_item, container, false);
ImageView mImageView = (ImageView) itemView.findViewById(R.id.imgViewCenterGallery);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap bitmap;
bitmap = BitmapFactory.decodeFile(mFiles[position], options);
mImageView.setImageBitmap(bitmap);
container.addView(itemView);
return itemView;
}
public int getItemPosition(Object object) {
return PagerAdapter.POSITION_NONE;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
}
答案 0 :(得分:1)
试试这种方式
if (imgLength > 0) {
mFiles = new String[imgLength];
mFileNames = new String[imgLength];
mUrls = new Uri[imgLength];
int j=0;
for (int i = imgLength-1; i >= 0; i--) {
mFiles[j] = imageList[i].getAbsolutePath();
mFileNames[j] = imageList[i].getName();
j++;
Toast.makeText(this, "Image Name - " + Arrays.toString(mFileNames), Toast.LENGTH_SHORT).show();
galleryViewPager = (ViewPager) findViewById(R.id.galleryViewPager);
adapter = new GallerySlideImageAdapter(this, mFiles, mFileNames);
galleryViewPager.setAdapter(adapter);
}
} else {
Toast.makeText(this, "Please capture some images from App !", Toast.LENGTH_SHORT).show();
}
答案 1 :(得分:0)
按照最新的日期时间对文件列表进行排序,并在适配器中绑定文件。
File[] files = directory.listFiles(); //your files object
Arrays.sort(files, new Comparator<File>(){
public int compare(File f1, File f2)
{
return Long.valueOf(f1.lastModified()).compareTo(f2.lastModified());
}
});