1

This is my very first post. Please correct me if there are any mistakes :)

I'm having a problem of displaying images (captured through my app) in a specific folder in Gallery. I've managed to saved it in my app's folder in the directory but when I proceed to the gallery, it won't show my app's folder (when first created) and images captured UNLESS I restart my phone.

I'm running on Android 4.4.4 and according to this , it seems to be a bug?

Anyway, here's my code:

private void savePic() {
        dir_path = "TravelBuddy";
        travelBuddyDir = new     File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), dir_path);
        timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        imgName = (timeStamp + ".jpg");
        output = null;
        File imagePath = new File(travelBuddyDir, imgName);

        try {
            output = new FileOutputStream(imagePath);
            bmp.compress(Bitmap.CompressFormat.JPEG, 100, output);
            output.flush();
            output.close();

            MediaStore.Images.Media.insertImage(getActivity().getContentResolver(), bmp,
                    imgName, imgName);
        } catch (Exception e) {
            e.printStackTrace();
        }    
}

Whereby the method above will be called in the onActivityResult(int requestCode, int resultCode, Intent data) method below:

public void onActivityResult(int requestCode, int resultCode, Intent data) {

        switch (resultCode) {
            case 1: {
                    if (resultCode == RESULT_OK) {
                    savePic();
        }
        break;
}

This is my method of checking and creating the directory before saving the image captured: (The outcome path: /storage/emulated/0/Pictures/TravelBuddy)

 private void checkDir() {
        if(Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
            dir_path = "TravelBuddy";
            travelBuddyDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), dir_path);

            if (!travelBuddyDir.exists() && (!travelBuddyDir.isDirectory())) {
                travelBuddyDir.mkdir();
                System.out.println("New directory has been created");

            } else {
                System.out.println("Directory already exists!");
            }
        } else {
            System.out.println("No storage available");
        }
    }

I'm new to this whole thing(capturing, storing, etc.) and have been trying for days. Any help is much appreciated. Thank you in advance!

Community
  • 1
  • 1
Angeline
  • 46
  • 1
  • 7
  • How are you starting camera? Also how are you trying to display it? Did you try to check if image was really saved to proper folder via file manager? – Divers Mar 22 '15 at 18:09
  • @Divers I'm starting via intent. I'm trying to display it as thumbnails like images in other folders in the gallery. Now that you mentioned it, using ES File Explorer, on the homepage I can access the folder by selecting Images or internal storage and both returned me two different paths. /storage/emulated/0/Pictures/TravelBuddy for the former and /sdcard/Pictures for the latter. Where supposedly /sdcard refers to /0. – Angeline Mar 22 '15 at 18:36
  • I didn't get if you can see image in "/storage/emulated/0/Pictures/TravelBuddy" folder or not, but seems problem that in 'onActivityResult' method in 'switch' you confused resultCode with requestCode. – Divers Mar 22 '15 at 19:52
  • @Divers Thank you for spotting that mistake! Yes, I can see image in "/storage/emulated/0/Pictures/TravelBuddy". After I corrected the mistake above, two images are saved to the directory at the same time: 1. A proper image that was captured and 2. an empty [image](http://i.imgur.com/gqL4bpk.png). The thumbnail shown in gallery is the empty image. Am I missing something here? P.s MediaScanner is now working. – Angeline Mar 23 '15 at 04:53

0 Answers0