Archie.bpgc
Archie.bpgc

Reputation: 24012

Why are images from newly created folder not showing up?

In my app i have an option to take a picture, which will be saved in the folder:

Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/my-images";

Images are being saved fine, and i also have an option to select image which uses this code:

Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), SELECT_PICTURE);

here initially it used to show 2 galleries

1. Camera

2. my-images

picking any of them etc.., all worked fine.

but for no reason i deleted the my-images folder and ran the application again. The same folder is created again and images are getting saved as expected.

The problem is:

my-images gallery is not showing up now, when i click the select images button.

Except for deleting the my-images folder and restarting the application, i haven't changed any code since it used to work well.

why is it so?

Thank you

EDIT:

my activity code, which allows user to take a picture and store on SD card, and browse image gallery:

public class PictureFromAppActivity extends SherlockActivity {
    private File dir;
    private Bitmap photo;
    private String encodedString;
    private InputStream is;
    private ImageView imagePreview;
    private String selectedImagePath;
    private static final int CAMERA_REQUEST = 1888;
    private static final int SELECT_PICTURE = 1;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        imagePreview = (ImageView) findViewById(R.id.image_preview);
        Button takePicture = (Button) findViewById(R.id.take_a_picture);
        Button selectImage = (Button) findViewById(R.id.select_picture);

        takePicture.setOnClickListener(new View.OnClickListener() {

            public void onClick(View arg0) {

                if (android.os.Environment.getExternalStorageState().equals(
                        android.os.Environment.MEDIA_MOUNTED)) {

                        Intent cameraIntent = new Intent(
                                android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                        startActivityForResult(cameraIntent, CAMERA_REQUEST);
                    }
            }
        });
        selectImage.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {

                Intent intent = new Intent();
                intent.setType("image/*");
                intent.setAction(Intent.ACTION_GET_CONTENT);
                startActivityForResult(
                        Intent.createChooser(intent, "Select Picture"),
                        SELECT_PICTURE);
            }
        });

    }

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

        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode == RESULT_OK) {
            if (requestCode == CAMERA_REQUEST) {
                if (data != null) {

                    photo = (Bitmap) data.getExtras().get("data");
                    Bitmap bitmapOrg = photo;
                    ByteArrayOutputStream bao = new ByteArrayOutputStream();
                    bitmapOrg.compress(Bitmap.CompressFormat.PNG, 100, bao);
                    byte[] ba = bao.toByteArray();

                    try {
                        encodedString = Base64.encodeBytes(ba, 0);
                    } catch (IOException e1) {
                    }

                    if (android.os.Environment.getExternalStorageState()
                            .equals(android.os.Environment.MEDIA_MOUNTED)) {

                        File imagesFolder = new File(
                                Environment
                                        .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
                                "/my-images");
                        if (!imagesFolder.exists()) {

                            imagesFolder.mkdirs();
                        } else {

                        }
                        String fileName = "image_" + count + ".jpg";
                        f = new File(imagesFolder, fileName);
                        while (f.exists()) {
                            count++;
                            fileName = "image_" + String.valueOf(count)
                                    + ".jpg";
                            f = new File(
                                    Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)
                                            + "/my-images", fileName);
                        }
                    } else {

                    }
                    f.createNewFile();

                    FileOutputStream fo = new FileOutputStream(f);
                    fo.write(ba);
                    o.flush();
                    fo.close();
                    try {
                        File imageFile = new File(
                                Environment
                                        .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)
                                        + "/my-images" + "/image_" + count + ".jpg");
                        Bitmap bitmap = BitmapFactory.decodeFile(imageFile
                                .getAbsolutePath());
                        imagePreview.setImageBitmap(bitmap);
                        Log.d("Image Width", "+" + imagePreview.getWidth());
                        Log.d("Image Height", "+" + imagePreview.getHeight());
                    } catch (Exception e) {

                    }
                } else {

                    Intent intent = new Intent(PictureFromAppActivity.this,
                            PictureFromAppActivity.class);
                    startActivity(intent);
                }
            } else if (requestCode == SELECT_PICTURE) {

                if (data != null) {
                    Uri selectedImageUri = data.getData();
                    selectedImagePath = getPath(selectedImageUri);
                    try {
                        File imageFile = new File(selectedImagePath);
                        Bitmap bitmap = BitmapFactory.decodeFile(imageFile
                                .getAbsolutePath());
                        imagePreview.setImageBitmap(bitmap);
                    } catch (Exception e) {

                    }

                } else {

                }
            }
        }
    }

    private String getPath(Uri selectedImageUri) {

         String[] projection = { MediaStore.Images.Media.DATA };
            Cursor cursor = managedQuery(selectedImageUri, projection, null, null, null);
            int column_index = cursor
                    .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            cursor.moveToFirst();
            return cursor.getString(column_index);
    }
}

Upvotes: 1

Views: 2902

Answers (1)

Abhinav Singh Maurya
Abhinav Singh Maurya

Reputation: 3313

May be that is because of the gallery is associated with your application and its keeping the view with it. So what is happening when you delete image/folder it been deleted from the system but the media gallery is not updated which is associated with your application. Please use below code and let me know if this solves your problem

sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,
                Uri.parse("file://"
                        + Environment.getExternalStorageDirectory())));

Please use this code to open images from gallery

intent = new Intent(
                    Intent.ACTION_PICK,
                    android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
            startActivityForResult(intent, PICK_PHOTO);

And in your on activity result

@Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        String path = "";
switch (requestCode) {
        case PICK_PHOTO:
            if (resultCode == RESULT_OK) {
               Uri uri = data.getData();
               path = pathFromUri(uri); // Now you can get the path of your selected image from here
           }
        break;
        }
    }

getting path from URI code

/**
     * Method to get path from uri
     * 
     * @param Uri
     *            uri
     * @return String path
     * */
    private String pathFromUri(Uri uri) {
        String[] filePathColumn = { MediaStore.Images.Media.DATA };
        Cursor cursor = getContentResolver().query(uri, filePathColumn, null,
                null, null);
        cursor.moveToFirst();
        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        String filePath = cursor.getString(columnIndex);
        cursor.close();
        return filePath;
    }

Upvotes: 5

Related Questions