自定义相机/图库应用程序无法准确显示SD卡/文件夹



我构建了一个自定义相机应用程序,可将图片保存到SD卡上的特定文件夹中,并具有自己的图库视图,用于查看该文件夹中的照片"/sdcard/myApp/images"

我一直在两种不同的平板电脑上进行测试,Galaxy Tab-1 10英寸和Galaxy Tab-2 10英寸。 相机在两台平板电脑上都可以正常工作,拍照并将它们保存到应用程序创建的文件夹中。 但是 Tab1 上的图库视图不会显示该文件夹中的所有照片。 tab-2 始终显示文件夹中当前的所有照片,但 tab-1 通常仅显示上次重新启动平板电脑时该文件夹中的图像。

因此,在 Tab-1 上,如果我在该文件夹中没有照片,然后用相机拍摄两张照片并切换到图库视图,则它不会显示任何照片。 如果我重新启动并转到图库视图,我会看到两张照片,但是如果我切换到相机并拍摄另一张照片,则图库仅显示原始的两张照片,直到我重新启动。

适用于安卓 3.2 +,但 3.1 不!

有什么想法吗?

游标发送到库视图构造函数中的适配器:

 // 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));

添加:

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;
    }

同样,这适用于选项卡-2(OS 4.0)和选项卡-1,因为公司软件已在选项卡-1和操作系统上从3.1更新到3.2,但为什么它不起作用。

尝试此操作以刷新媒体文件。

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

最新更新