简体   繁体   English

在AyncTask中解码BLOB,并将BLOB字符串作为参数传递

[英]Decode BLOB in AyncTask passing in BLOB string as parameter

Goal 目标

My Goal here is to download an image from my server. 我的目标是从服务器下载图像。 HOWEVER, the image as a blob field will be stored in the database and not a link to the image. 但是,图像作为Blob字段将存储在数据库中,而不是图像的链接。

What I have done 我做了什么

I actually had this downloading of image working but the path to the image was passed and that image was downloaded from the file. 我实际上可以下载图像,但是通过了图像的路径,并且从文件中下载了该图像。 However, I am attempting to edit my code so it takes the Blob string (downloaded from the server)and loads this image. 但是,我试图编辑我的代码,以便它使用Blob字符串(从服务器下载)并加载此图像。 Below is my code. 下面是我的代码。

What I think the problem is 我认为问题是

My biggest issue is in the try I take my "blob" in as a string parameter. 我最大的问题是尝试将“ blob”作为字符串参数。 (IS that mistake number 1?) which is urldisplay. (是那个错误编号1?),它是urldisplay。 however , BitmapFactory.decodeStream() requires an InputStream. 但是, BitmapFactory.decodeStream()需要一个InputStream。 but if I change my parameter from a String to InputStream. 但是如果我将参数从String更改为InputStream。

I get an error on the following code saying :cannot resolve method(java.lang.string) 我在以下代码上收到一条错误消息:无法解析方法(java.lang.string)

 new DownloadImageTask(holder.imageview).execute(ArrayListStudents.get(position).getImage());

and if I change all my settings and getters of images to InputStream, I get an error where I call student.setImage(jRealObject.getString("cimage")); 如果我将所有图像的设置和获取器更改为InputStream,则会在调用student.setImage(jRealObject.getString("cimage"));地方出现错误student.setImage(jRealObject.getString("cimage")); to get the data from the column on the "GetString" 从“ GetString”上的列获取数据

private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
    ImageView bmImage;

    public DownloadImageTask(ImageView bmImage) {
        this.bmImage = bmImage;
    }

    protected Bitmap doInBackground(String... urls) {
        String urldisplay = urls[0];// this parameter once had url of image  

        //but now it has the image bitmap.
        Bitmap cImg1= null;
        try {
            InputStream in =  new java.net.URL(urldisplay).openStream();
            // cImg1= BitmapFactory.decodeStream(in);
            cImg1=urldisplay;//Assign strings to BitMap?
        } catch (Exception e) {
            Log.e("Error", e.getMessage());
            e.printStackTrace();
        }
        return cImg1;
    }  

    protected void onPostExecute(Bitmap result) {
        bmImage.setImageBitmap(result);
    }
}

Use this to decode your BLOB to bitmap : 使用它来将BLOB解码为位图:

protected Bitmap doInBackground(String... urls) {
    String urldisplay = urls[0];// this parameter once had url of image  
    Bitmap cImg1= null;
    try {
        byte[] byteArray = DBcursor.getBlob(urldisplay);  
        cImg1 = BitmapFactory.decodeByteArray(byteArray, 0 ,byteArray.length);
    } catch (Exception e) {
        Log.e("Error", e.getMessage());
        e.printStackTrace();
    }
    return cImg1;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM