GridView重命名文件名

时间:2012-01-06 02:32:17

标签: android

我使用下面的代码在GridView中显示照片:

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

上面的onCreate()。 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));
}

第一次,对话框显示正确的文件名,并重命名文件名成功。 但第二次,对话框显示未重命名的旧文件名,并且重命名失败。 如何修改以避免它?

我尝试重新启动应用程序,它仍显示旧文件名。

2 个答案:

答案 0 :(得分:1)

您只修改文件名但没有修改数据库中的文件名,您需要刷新数据库以确保重命名。

你可以尝试这个来刷新数据库:

    Intent scanIntent = new Intent(Intent.ACTION_MEDIA_MOUNTED);
    scanIntent.setData(Uri.fromFile(new File(Environment.getExternalStorageDirectory().getPath())));
    context.sendBroadcast(scanIntent);

将安装存储,并扫描媒体。

答案 1 :(得分:1)

我认为您需要通过从数据库获取数据并在数据库中重命名和更新后调用它来重新绘制Grid View方法。

编辑:

onCreate(){  
...............  
............  
drawGrid();  
}


public void getData(){  
File.clear();  
Path.clear();    
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);   
}}

public void drawGrid(){  
getData();  
gv = (GridView)findViewById(R.id.phone_photo);  
AAdapter = new AAdapter(this, FilePath, FileThumb);  
gv.setAdapter(AAdapter);  
}

单击AlertDialog“OK”时调用onDraw() 我认为它有效..