从光标设置ImageView

时间:2013-06-12 08:24:27

标签: android

我正在尝试从光标返回的Uri(在sqlite数据库列中)设置imageView但它不起作用。

代码:

mCursor = mydb.rawQuery("select * from events;", null);
 if (mCursor != null ) {
     mCursor.moveToFirst();

String[] from = new String[]{DbHandler.column_id, DbHandler.column_name, DbHandler.column_location, DbHandler.column_date, DbHandler.img_loc};
int[] to = new int[] {R.id.textView1, R.id.textView2, R.id.textView3, R.id.textView4, R.id.photoInDb};
SimpleCursorAdapter simpleCurs = new SimpleCursorAdapter(this, R.layout.listviewfinal, mCursor, from, to);
 simpleCurs.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
                @Override
                public boolean setViewValue(View view, Cursor cursor, int i) {

                if (view instanceof ImageView) {

                    ImageView image = (ImageView) view;
                    // just to see what is returned by this!
                    Log.d("orange", "images: " + cursor.getString(i));
                    Bitmap thumbImage = ThumbnailUtils.extractThumbnail(BitmapFactory.decodeFile(cursor.getString(i)), 320, 240);
                    image.setImageBitmap(thumbImage);
                    return true;
                }
                return false;
                }
            });
            listView.setAdapter(simpleCurs);

有什么想法吗?

2 个答案:

答案 0 :(得分:1)

尝试此代码,如果您正确获取图像路径,它应该可以正常工作。

Bitmap thumbImage = BitmapFactory.decodeFile(imagePath);
image.setImageBitmap(thumbImage );

修改

我认为您在获取图像路径时遇到了一些问题。按照以下步骤获取路径。

String imagePath= Environment.getExternalStorageDirectory().toString() "folderpath/imageaname.jpg";

在您的情况下,文件夹路径为 DCIM / OrangeClubPhotos / ,图片名称为 1370853592867.jpg

答案 1 :(得分:0)

撰写这些简单的行

URL url = new URL("http://image10.bizrate-images.com/resize?sq=60&uid=2216744464");
Bitmap bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
imageView.setImageBitmap(bmp);

或试试这个

File imgFile = new  File(“/sdcard/Images/test_image.jpg”);
if(imgFile.exists()){

    Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());

    ImageView myImage = (ImageView) findViewById(R.id.imageviewTest);
    myImage.setImageBitmap(myBitmap);

}