2014-12-05 77 views
0

对不起,我的英语。ImageView中的Android图像文件夹

我想导入智能手机的图像文件夹。我想用这些图像做一个画廊。 我的问题是当我把图像放入数组后,我无法将数组的每个图像放在ImageView中。

这是我使用画廊

public class SeeGallery extends Activity { 

File[] listFile ; 

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_see_gallery); 

    File folder = new File(Environment.getExternalStorageDirectory() + "/ProjectKozaImages"); 
    listFile = folder.listFiles(); 

    // Note that Gallery view is deprecated in Android 4.1--- 
    Gallery gallery = (Gallery) findViewById(R.id.gallery1); 
    gallery.setAdapter(new ImageAdapter(this)); 
    gallery.setOnItemClickListener(new OnItemClickListener() { 
    public void onItemClick(AdapterView<?> parent, View v, int position,long id) 
    { 
     // display the images selected 
     ImageView imageView = (ImageView) findViewById(R.id.image1); 
     imageView.setImageResource(listFile[position]); 
     } 
     }); 
    } 

public class ImageAdapter extends BaseAdapter { 
    private Context context; 
    private int itemBackground; 
    public ImageAdapter(Context c) 
    { 
    context = c; 
    // sets a grey background; wraps around the images 
    TypedArray a =obtainStyledAttributes(R.styleable.MyGallery); 
    itemBackground = a.getResourceId(R.styleable.MyGallery_android_galleryItemBackground, 0); 
    a.recycle(); 
    } 
    // returns the number of images 
    public int getCount() { 
    return listFile.length; 
    } 
    // returns the ID of an item 
    public Object getItem(int position) { 
    return position; 
    } 
    // returns the ID of an item 
    public long getItemId(int position) { 
    return position; 
    } 
    // returns an ImageView view 
    public View getView(int position, View convertView, ViewGroup parent) { 
    ImageView imageView = new ImageView(context); 
    imageView.setImageResource(listFile[position]); 
    imageView.setLayoutParams(new Gallery.LayoutParams(100, 100)); 
    imageView.setBackgroundResource(itemBackground); 
    return imageView; 
    } 
    } 

预先感谢您的帮助代码

回答

0

我认为你需要存储在FileArray文件路径先转换为位图...

Bitmap bitmap = BitmapFactory.decodeFile(listFile[position].getAbsolutePath()); 
imageView.setImageBitmap(bitmap);