gpt4 book ai didi

Android View Pager 从文件夹中删除图像不刷新

转载 作者:行者123 更新时间:2023-11-30 00:43:56 25 4
gpt4 key购买 nike

每当我从 ViewPager 中删除图像时。然后有时下一张图片会被删除。或者有时没有图像删除。

我不知道哪里错了。

我的画廊 Activity

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;
}
}

下面是我的Viewpager的GallaryAdapter

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;
}
}

最佳答案

试试这个

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();
}

关于Android View Pager 从文件夹中删除图像不刷新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42108440/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com