我使用下面的代碼在 GridView 中 onCreate()方法中顯示照片:
String[] projection = {MediaStore.Images.Media._ID, MediaStore.Images.Media.DATA, MediaStore.Images.ImageColumns.DATA};
Cursor cursor = managedQuery( MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection, null, null, MediaStore.Images.Media._ID);
int count = cursor.getCount();
int image_column_index = cursor.getColumnIndex(MediaStore.Images.Media._ID);
int image_path_index = cursor.getColumnIndex(MediaStore.Images.Media.DATA);
fd.clearPhonePhoto();
int i;
for(i = 0; i < count; i++) {
cursor.moveToPosition(i);
int id = cursor.getInt(image_column_index);
String p = cursor.getString(image_path_index);
Bitmap b = MediaStore.Images.Thumbnails.getThumbnail(getApplicationContext().getContentResolver(), id, MediaStore.Images.Thumbnails.MICRO_KIND, null);
FilePath.add(p);
FileThumb.add(b);
}
b4 = (Button)findViewById(R.id.btn_4);
b4.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
int i;
for(i = 0; i < Check.length; i++) {
if(Check[i]) {
AlertDialog.Builder renameDialog = new AlertDialog.Builder(PhonePhotoListActivity.this);
renameDialog.setTitle("Rename");
final EditText name = new EditText(AActivity.this);
name.setText(FilePath.get(i).substring(FilePath.get(i).lastIndexOf("/") + 1)); //set old file name
renameDialog.setView(name);
final String oripath = FilePath.get(i);
renameDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
rename(oripath, name.getText().toString());
Intent it = new Intent(AActivity.this, AActivity.class);
it.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(it);
}
});
renameDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {}
});
renameDialog.show();
}
}
}
});
gv = (GridView)findViewById(R.id.phone_photo);
AAdapter = new AAdapter(this, FilePath, FileThumb);
gv.setAdapter(AAdapter);
rename()方法:
public void rename(String ori, String edit) {
File rename = new File(ori);
String newpath = ori.substring(0, ori.lastIndexOf("/") + 1) + edit;
rename.renameTo(new File(newpath));
}
第一次,dialog顯示正確的文件名,也能重新命名文件名。但是第二次,dialog還是顯示舊的文件名,也沒有改過名稱,也不能重命名。我重啟程序還是顯示舊的文件名。
如何處理這個問題呢?
注意兩點:
第一,如果是Android的SD卡上的文件重命名,那麼必須添加權限:
第二,oldPath和newPath必須是新舊文件的絕對路徑
此外你說的這個修改後notifyDataChange一下