Click here to Skip to main content
15,915,603 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Java
 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();
    }

}
Posted
Updated 22-Dec-15 23:18pm
v4
Comments
Richard MacCutchan 23-Dec-15 4:00am    
Please edit your post, add <pre> tags around your code, and explain where the error occurs.
Sonia Wadji 23-Dec-15 4:50am    
@Richard I added the tags. This doesnt show any errors. It simply shows a blank entry in the listview for that image.
Richard MacCutchan 23-Dec-15 5:19am    
I corrected the tags to exclude the preamble text, and changed the language selector from C# to Java.
Richard MacCutchan 23-Dec-15 5:20am    
Where is the image, where does it get added to the ListView? Have you run this in the debugger to see what is happening?
Sonia Wadji 23-Dec-15 5:48am    
When I select the image from the gallery, it gets moved from that folder to Fizzzzz folder using the moveFile function. But this image which is there in the Fizzzz folder is not getting displayed in the listview.
Using this line - images.add(image);
the image gets added to the listview!

1 solution

I would add the following

Java
}finally {
                        //reopening/ redirecting the mainActivity to itself
                       Intent i = new Intent(this,MainActivity.class);
                        startActivity(i);
                        adapter.notifyDataSetChanged(); // Notify change here
                        dialog.cancel();


Hope it helps

/Darren
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900