Search code examples
androidandroid-sdcardgalaxy-tab

Custom Camera/Gallery app not showing sdcard/folder accurately


I have built a custom camera app that saves pictures to a specific folder on my sdcard and has its own gallery view for viewing the photos in that folder "/sdcard/myApp/images"

I have been testing it on two different tablets, Galaxy Tab-1 10 inch and Galaxy Tab-2 10 inch. The camera works on both tablets just fine, taking pictures and saving them to the app created folder. But the gallery View on the Tab1, doesn't show all the photos in that folder. The tab-2 always shows all the photos currently in the folder but the tab-1 often only shows the images in that folder from the last time the tablet was rebooted.

So on the Tab-1 if i have no photos in that folder and then take two pictures with the camera and switch to the gallery view, it shows no photos. If i then reboot, and go to the gallery view i see both of the photos, but if i switch to the camera and take another photo the gallery only shows the original two until i reboot.

works on android 3.2 + but 3.1 nope!

Any ideas?

cursor sent to adapter in constructor of galleryView:

 // Set up an array of the Thumbnail Image ID column we want
    String[] projection = {MediaStore.Images.Media._ID};
    // Create the cursor pointing to the SDCard
    cursor = managedQuery( MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
            projection, // Which columns to return
            MediaStore.Images.Media.DATA + " like ? ",
            new String[] {"%LC/images%"},  
            MediaStore.Images.Media._ID + " DESC");


    // Get the column index of the Thumbnails Image ID
    columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media._ID);


    ga.setAdapter(new GallAdapter(this,cursor,columnIndex));

addapter:

public class GallAdapter extends BaseAdapter {
    public Cursor cursor;
    private int columnIndex;
    private Context context;
    int imageBackground;
    String url;
    Uri uri;
    int originalImageId;
    int imageID;
    int columnData;
    ViewGroup myp;
    ImageView d;

    public GallAdapter(Context ctx, Cursor cur, int cIn ) {
        context = ctx;
        columnIndex = cIn;

        cursor = cur;
        Log.v("GallAdapter", "COUNT:"+getCount());
    }

    @Override
    public int getCount() {

        return cursor.getCount();
    }

    @Override
    public Object getItem(int position) {

        return position;
    }

    @Override
    public long getItemId(int position) {

        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        myp = parent;
        View v;
        if(convertView ==null){
            v = LayoutInflater.from(context).inflate(R.layout.galitem, parent, false);
        }else{
            v = convertView;
        }


        ImageView photo = (ImageView) v.findViewById(R.id.imageView);
        ImageView border = (ImageView) v.findViewById(R.id.borderView);
        d = (ImageView) v.findViewById(R.id.delView);



        // Move cursor to current position
        cursor.moveToPosition(position);

        // Get the current value for the requested column
        imageID = cursor.getInt(columnIndex);
        // obtain the image URI
        uri = Uri.withAppendedPath( MediaStore.Images.Media.EXTERNAL_CONTENT_URI, Integer.toString(imageID) );
        url = uri.toString();
        // Set the content of the image based on the image URI
        originalImageId = Integer.parseInt(url.substring(url.lastIndexOf("/") + 1, url.length()));
        Bitmap b = MediaStore.Images.Thumbnails.getThumbnail(context.getContentResolver(),
                        originalImageId, MediaStore.Images.Thumbnails.MINI_KIND, null);
        photo.setImageBitmap(b);

        photo.setScaleType(ImageView.ScaleType.FIT_CENTER); 
        d.setTag(uri);
        d.setOnClickListener(new OnClickListener(){


            public void onClick(View v) {

                String path = getRealPathFromURI((Uri) v.getTag());
                alertBox("Warning!", "Are you sure you want to delete this photo?", path, v);


            }


        });

        return v;
    }

again this works on tab-2 (os 4.0) and tab-1 now that the firm ware was updated on the tab-1 and the os from 3.1 to 3.2 but why was it not working..


Solution

  • Try this in order to refresh your media files.

    this._context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + Environment.getExternalStorageDirectory() + "/Hugg/" + name)));