отображать изображения, используя URL из Sqlite DB - PullRequest
0 голосов
/ 19 сентября 2011

Я хочу сохранить изображения с определенного URL в базу данных sqlite и затем отобразить его из базы данных .. Может кто-нибудь сказать мне, как, как я мог сделай это.

Заранее спасибо.

С уважением, Рагхав Раджагопалан

1 Ответ

0 голосов
/ 19 сентября 2011

попробуйте этот код.

   URL url = new URL("your image url");
    URLConnection ucon = url.openConnection();
    InputStream is = ucon.getInputStream();
        BufferedInputStream bis = new BufferedInputStream(is,128);
        ByteArrayBuffer barb= new ByteArrayBuffer(128);
         //read the bytes one by one and append it into the ByteArrayBuffer barb
         int current = 0;
         while ((current = bis.read()) != -1) {
                 barb.append((byte) current);
         }

         ContentValues filedata= new ContentValues();
        //TABLE_FIELD = a field in the database table of type text
         filedata.put(TABLE_FIELD,barb.toByteArray());
        //TABLE_NAME = a table in the database with a field TABLE_FIELD 
         db.insert(TABLE_NAME, null, filedata);

            //Retriving Data from DB:
    //select the data
    Cursor cursor = db.query(TABLE_STATIONLIST, new String[] {TABLE_FIELD},null, null, null, null, null);
    //get it as a ByteArray
           //reading as Binary large object(BLOB)
    byte[] imageByteArray=cursor.getBlob(1);

    //the cursor is not needed anymore so release it
    cursor.close();


Bitmap image=getbyteAsBitmap(imageByteArray);



//use this method to convert byte[] to bitmap


    private  Bitmap getbyteAsBitmap(byte[] bytedata){
            if(bytedata!=null){
                ByteArrayInputStream imageStream = new ByteArrayInputStream(bytedata);
                Bitmap theImage = BitmapFactory.decodeStream(imageStream);
                //theImage=MediaProcesser.getRoundedCornerBitmap(theImage, 5);
                return theImage;
            }else{
                return null;
            }
        }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...