如何创建SD卡上有一个特定文件夹的图片自定义库? [英] How to create a custom gallery having images of a particular folder on SD card?

查看:199
本文介绍了如何创建SD卡上有一个特定文件夹的图片自定义库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我见过的Andr​​oid(/ApiDemos/src/com/example/android/apis/view/Gallery1.java)的API演示,但他们采取项目距离res文件夹的图像。我想创建它们竖起文件夹中的图片画廊: /mnt/sdcard/Android/data/com.myapp/files/Pictures /

I have seen the API demos of android (/ApiDemos/src/com/example/android/apis/view/Gallery1.java) , but they take images from the res folder within the project. I want to create a gallery of the images which are put up in the folder: /mnt/sdcard/Android/data/com.myapp/files/Pictures/

所有我能想出是这样的code,其中,我想,显示所有图像。

All i could come up was this code, which, i suppose, displays all images.

public class ExistingPicGallery extends Activity {

private Cursor cursor;
private int columnIndex;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.gallery1);
    Gallery g=(Gallery)findViewById(R.id.gallery1);
    String[] projection = {MediaStore.Images.ImageColumns._ID,MediaStore.Images.Thumbnails.IMAGE_ID, 
            MediaStore.Images.Thumbnails.KIND};
    // Create the cursor pointing to the SDCard
    String selection = MediaStore.Images.Thumbnails.KIND +
            "="  + // Select only mini's 
                            MediaStore.Images.Thumbnails.MINI_KIND; 
    cursor = managedQuery( MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI,
            projection, 
            selection,
            null,
            null);
    // Get the column index of the image ID
    columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media._ID);
    g.setAdapter(new ImageAdapter(this));

    g.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView parent, View v, int position, long id) {
            Toast.makeText(ExistingPicGallery.this, "" + position, Toast.LENGTH_SHORT).show();
        }
    });

}



 public class ImageAdapter extends BaseAdapter {

     int mGalleryItemBackground;

      public ImageAdapter(Context c) {
            context = c;

            TypedArray a = obtainStyledAttributes(R.styleable.ExistingPicGallery);
            mGalleryItemBackground = a.getResourceId(
                    R.styleable.ExistingPicGallery_android_galleryItemBackground, 0);
            a.recycle();
        }

        public int getCount() {
            return cursor.getCount();
        }

        public Object getItem(int position) {
            return position;
        }

        public long getItemId(int position) {
            return position;
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            ImageView i = new ImageView(context);
            // Move cursor to current position
            cursor.moveToPosition(position);
            // Get the current value for the requested column
            int imageID = cursor.getInt(columnIndex);
            // obtain the image URI
            Uri uri = Uri.withAppendedPath( MediaStore.Images.Media.EXTERNAL_CONTENT_URI, Integer.toString(imageID) );
            String url = uri.toString();
            // Set the content of the image based on the image URI
            int originalImageId = Integer.parseInt(url.substring(url.lastIndexOf("/") + 1, url.length()));
            Bitmap b = MediaStore.Images.Thumbnails.getThumbnail(getContentResolver(),
                            originalImageId, MediaStore.Images.Thumbnails.MINI_KIND, null);
            i.setImageBitmap(b);
            i.setLayoutParams(new Gallery.LayoutParams(150, 100));
            i.setScaleType(ImageView.ScaleType.FIT_XY);
            i.setBackgroundResource(0);
            return i;
        }

        private Context context;


        ;
    }
}

我花了很多时间寻找的解决方案,但没有成功。

I have spent a lot of time looking for its solution, but no success..

推荐答案

我已经按照/ApiDemos/src/com/example/android/apis/view/Gallery1.java变化

I have made following changes in /ApiDemos/src/com/example/android/apis/view/Gallery1.java

public class ImageAdapter extends BaseAdapter {
    int mGalleryItemBackground;

    public ImageAdapter(Context c) {
        mContext = c;

    }

    public int getCount() {
        return allFiles.length;
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(final int position, View convertView,
            ViewGroup parent) {

        ImageView myImageView = new ImageView(mContext);

        if (convertView != null)
            myImageView = (ImageView) convertView;
        else {
            myImageView = new ImageView(mContext);
            myImageView.setLayoutParams(new GridView.LayoutParams(60, 60));
            myImageView.setAdjustViewBounds(false);
            myImageView.setScaleType(ImageView.ScaleType.CENTER_CROP);

        }

        Bitmap bitmapImage = BitmapFactory.decodeFile(folder + "/"
                + allFiles[position]);
        BitmapDrawable drawableImage = new BitmapDrawable(bitmapImage);
        myImageView.setImageDrawable(drawableImage);

        return myImageView;

    }

    private Context mContext;

    File folder = new File(
            Environment.getExternalStorageDirectory()
            .getPath()+"/files/Pictures/");
            String[] allFiles = folder.list();


}

因此​​,我们得到这个文件夹中的图片名称的数组。在示例中,我们得到了可绘制的ID数组。

Hence, we get array of image names in this folder. In the sample, we got the array of IDs of the drawables.

这篇关于如何创建SD卡上有一个特定文件夹的图片自定义库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆