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";
Uri selectedImageUri = data.getData();
selectedImagePath = getPath(selectedImageUri);
Toast.makeText(getApplication(),selectedImagePath,Toast.LENGTH_SHORT).show();
File mediaStored = new File(getPath(selectedImageUri));
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM), "Fizzzzzzz");
if (!mediaStorageDir.exists()) {
if (!mediaStorageDir.mkdirs()) {
Log.d("Fizzzzzzz", "failed to create directory");
Toast.makeText(getBaseContext(),"File directory creation failed",Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(getApplication(),"Creating & Transferring",Toast.LENGTH_SHORT).show();
try {
Toast.makeText(getApplication(),"Entering copyFile",Toast.LENGTH_SHORT).show();
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);
images.add(image);
daOdb.addImage(image);
adapter.notifyDataSetChanged();
list.invalidateViews();
dialog.cancel();
}catch (IOException e){
e.printStackTrace();
}finally {
}
}
}else{
try {
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);
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 {
Intent i = new Intent(this,MainActivity.class);
startActivity(i);
dialog.cancel();
}
}
}
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();
}
}