本文介绍了将图像从一个文件夹移动到另一个文件夹但不在列表视图中显示。 (机器人)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在此,我成功地将图像移动到另一个文件夹,但它在列表视图中显示了移动图像的空白条目。我在这里使用SQLite DB来保存图像名称,路径和描述。请帮我。
代码如下:
OnActivityResult
In this, I am successfully able to move the image to another folder, but it is displaying a blank entry in the listview for the moved image. I am using SQLite DB here in order to save the image name, path and description. Please help me.
Code is given below :
OnActivityResult
case RESULT_LOAD_IMAGE:
if (requestCode == SELECT_PICTURE &&
resultCode == RESULT_OK && null != data) {
final Dialog dialog = new Dialog(this);
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String fname = "IMG_"+ timeStamp + ".jpg";
//Selected Image Uri
Uri selectedImageUri = data.getData();
selectedImagePath = getPath(selectedImageUri);
Toast.makeText(getApplication(),selectedImagePath,Toast.LENGTH_SHORT).show();
File mediaStored = new File(getPath(selectedImageUri));//Source file
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM), "Fizzzzzzz");//creates folder FizzApp1 in Pictures directory
// This location works best if you want the created images to be shared
// between applications and persist after your app has been uninstalled.
// Create the storage directory if it does not exist
if (!mediaStorageDir.exists()) {
if (!mediaStorageDir.mkdirs()) {
Log.d("Fizzzzzzz", "failed to create directory");
Toast.makeText(getBaseContext(),"File directory creation failed",Toast.LENGTH_SHORT).show();
//return null;
}else{
Toast.makeText(getApplication(),"Creating & Transferring",Toast.LENGTH_SHORT).show();
//copyFileOrDirectory(getPath(selectedImageUri), "FizzApp1");
try {
Toast.makeText(getApplication(),"Entering copyFile",Toast.LENGTH_SHORT).show();
//copyFile(mediaStored, mediaStorageDir);
moveFile(mediaStored,mediaStorageDir);
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver()
.query(selectedImageUri, filePathColumn, null, null,
null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
MyImage image = new MyImage();
image.setTitle(fname);
image.setDescription(picturePath);
image.setDatetime(System.currentTimeMillis());
image.setPath(picturePath);
//Toast.makeText(getApplication(),picturePath,Toast.LENGTH_SHORT).show();
images.add(image);
daOdb.addImage(image);
adapter.notifyDataSetChanged();
list.invalidateViews();
dialog.cancel();
}catch (IOException e){
e.printStackTrace();
}finally {
}
}
}else{
try {
//copyFile(mediaStored, mediaStorageDir);
moveFile(mediaStored,mediaStorageDir);
Toast.makeText(getBaseContext(),"Moving Image",Toast.LENGTH_SHORT).show();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver()
.query(selectedImageUri, filePathColumn, null, null,null);//I think the query needs to be changed.
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
MyImage image = new MyImage();
image.setTitle(fname);
image.setDescription(" ");
image.setDatetime(System.currentTimeMillis());
image.setPath(picturePath);
images.add(image);
daOdb.addImage(image);
adapter.notifyDataSetChanged();
list.invalidateViews();
dialog.cancel();
}catch (IOException e){
e.printStackTrace();
}finally {
//reopening/ redirecting the mainActivity to itself
Intent i = new Intent(this,MainActivity.class);
startActivity(i);
dialog.cancel();
}
}
}
// MoveFile Function
private void moveFile(File file, File dir) throws IOException {
File newFile = new File(dir, file.getName());
FileChannel outputChannel = null;
FileChannel inputChannel = null;
try {
outputChannel = new FileOutputStream(newFile).getChannel();
inputChannel = new FileInputStream(file).getChannel();
inputChannel.transferTo(0, inputChannel.size(), outputChannel);
inputChannel.close();
file.delete();
} finally {
if (inputChannel != null) inputChannel.close();
if (outputChannel != null) outputChannel.close();
}
}
推荐答案
}finally {
//reopening/ redirecting the mainActivity to itself
Intent i = new Intent(this,MainActivity.class);
startActivity(i);
adapter.notifyDataSetChanged(); // Notify change here
dialog.cancel();
希望有帮助
/ Darren
Hope it helps
/Darren
这篇关于将图像从一个文件夹移动到另一个文件夹但不在列表视图中显示。 (机器人)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!