从firebase获取图像并插入SQLite并显示到图像视图中...

时间:2017-01-09 09:00:18

标签: android sqlite firebase firebase-storage

我正在尝试从Firebase数据库中的DownloadURl中获取Firebase存储中的图片..

Firebase结构

用户

|的 _9304809841

    |__IMAGE :"https://firebasestorage.googleapis.com/v0/b/zumi- 
              60a8f.appspot.com/o/Profile_Image%2Fcropped-122662379.jpg?
             alt=media&token=c8f3e9ee-637d-4bdd-9a76-a186ecd07e37"

我使用下面的代码从URL获取图像..

final DefaultHttpClient client = new DefaultHttpClient();
            final HttpGet getRequest = new HttpGet(URL);  //FETCHING THE URL FROM FIREBASE DATABASE..
            try {

                HttpResponse response = client.execute(getRequest);

                //check 200 OK for success
                final int statusCode = response.getStatusLine().getStatusCode();

                if (statusCode != HttpStatus.SC_OK) {
                    //Toast.makeText(MainActivity.this, "Error in HttpSTATUS", Toast.LENGTH_SHORT).show();
                    //return;

                }

                final HttpEntity entity = response.getEntity();
                if (entity != null) {
                    InputStream inputStream = null;
                    try {
                        // getting contents from the stream
                        inputStream = entity.getContent();
                        //inputStream = (InputStream) new URL(params[0].toString()).getContent();

                        byte[] image = new byte[inputStream.available()];
                        inputStream.read(image);
                        values.put("image", image); //inserting the Values into ContentVAlues



                    } finally {
                        if (inputStream != null) {
                            inputStream.close();
                        }
                        entity.consumeContent();
                    }
                    if (values != null) {
                        Cdb.insert("current_Luser", null, values); // INSERTING INTO SQLite DB
                    }
                }
                else
                {
                    //Toast.makeText(MainActivity.this, "Entity is Null", Toast.LENGTH_SHORT).show();
                }
            } catch (Exception e) {
                // You Could provide a more explicit error message for IOException
                getRequest.abort();
                Log.d("",e.toString());
               // Toast.makeText(MainActivity.this, "Error "+e.toString(), Toast.LENGTH_SHORT).show();
            }

            return null;
        }

    } 

我正在尝试使用

检索图像
Cursor c=Cdb.rawQuery("select email_phone,status,Dname,image from current_Luser",null);

        Toast.makeText(this, "No of record : "+c.getCount(), Toast.LENGTH_SHORT).show();
        if(c.moveToNext()) {

            if (c.getCount() > 0) {
                email.setText(c.getString(0).replace("@zumi.com", "").toString());
                name.setText(c.getString(2));
                status.setText(c.getString(1));
                byte[] img = c.getBlob(3);
                Bitmap bmp = BitmapFactory.decodeByteArray(img, 0, img.length);
                showpotos.setImageBitmap(bmp);

             }

将数据作为[B @ d0ca6a3值插入到SQLite DB中...但它不会显示在图像视图中....

请帮助..

1 个答案:

答案 0 :(得分:0)

在此声明中使用available()可能会导致问题:

byte[] image = new byte[inputStream.available()]

documentation for available()说:

  

请注意,虽然InputStream的某些实现将返回   流中的总字节数,很多都不会。永远不会   正确使用此方法的返回值来分配缓冲区   旨在保存此流中的所有数据