我正在尝试删除网格视图中的项目/文件。这些文件位于/data/my-image-folder/
. 文件会立即删除,但 UI 不会立即更新。这是我的代码:
imageFilePath = /data/<my-image-folder>/"file.jpg";
File file = new File(imageFilePath);
if (file.exists()) {
boolean deleted = file.delete();
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,Uri.parse(imageFilePath+ Environment.getExternalStorageDirectory())));
获取查看代码:
View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View gridView;
if (convertView == null) {
gridView = new View(context);
// get layout from gridlayout.xml
gridView = inflater.inflate(R.layout.gridlayout, null);
// set value into textview
TextView textView = (TextView) gridView
.findViewById(R.id.grid_item_label);
textView.setText(fileList[position]);
System.out.println("value of fileList[position]" + fileList[0]);
// set image
ImageView imageThumbnail = (ImageView) gridView
.findViewById(R.id.grid_item_image);
byte[] imageData = null;
try {
final int THUMBNAIL_SIZE = 64;
FileInputStream fis = new FileInputStream(FILE_PATH
+ fileList[position]);
Bitmap imageBitmap = BitmapFactory.decodeStream(fis);
Float width = new Float(imageBitmap.getWidth());
Float height = new Float(imageBitmap.getHeight());
Float ratio = width / height;
imageBitmap = Bitmap.createScaledBitmap(imageBitmap,
(int) (THUMBNAIL_SIZE * ratio), THUMBNAIL_SIZE,
false);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
imageBitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
imageData = baos.toByteArray();
imageThumbnail.setImageBitmap(imageBitmap);
} catch (Exception ex) {
}
} else {
gridView = (View) convertView;
}
return gridView;
}
这是我的网格视图适配器代码:
ImageAdapter adapter = null; //ImageAdapter extends BaseAdapter
if (fileList != null) {
adapter = new ImageAdapter(this, fileList);
gridView.setAdapter(i);
}
如果我这样做,则在删除后:
adapter.notifyDataChanged();
grid.invalidateViews();
编译器抱怨将适配器设为“最终”,但如果我将其设为“最终”,它会抱怨我无法使适配器成为最终的,因为它不允许以下行发生:
adapter = new ImageAdapter(this, fileList);
此外,我找不到实现notifyDataChanged。有notifyDataSetChanged、notify、notifyAll和notifyDataSetInvalidated但没有notifyDataChanged。
我认为 Intent.ACTION_MEDIA_MOUNTED 适用于位于外部 sd 卡上的所有图像/文件,而不适用于手机文件系统。这是正确的。
我怎样才能实现它。请建议。
Rgds,软软的