0

在我的 Android 应用程序中,我正在调用一个 Java Web 服务,它返回一个 Image Blob。我想在 ImageView 中将其显示为位图。此图像 blob 是 HTML5 Canvas 元素的已保存 toDataUrl() 表示。主线程将 AsyncTask 内部类作为带有执行命令的 onclick 启动。这是主线程:

    public class MainActivity extends Activity
    {
        private ImageView outputIv;
        public View vue;

        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            setOutputIv((ImageView)findViewById(R.id.imageView1));
        }

        public void onClickBtn(View v){

            vue = v;
            new RequestTask(v).execute("http://10.0.2.2:8080/CasosDeUso5/webresources/casosdeusowsport/getimagenes?usuarioId=1");
        }

        /**
         * @return the outputIv
         */
        public ImageView getOutputIv() {
            return outputIv;
        }

        /**
         * @param outputIv the outputIv to set
         */
        public void setOutputIv(ImageView outputIv) {
            this.outputIv = outputIv;
        }


        class RequestTask extends AsyncTask<String, String, String>{

            private View rootView;

            public RequestTask(View rootView){
            this.rootView=rootView;
        }

        @Override
        protected String doInBackground(String... uri) {
            HttpClient httpclient = new DefaultHttpClient();
            HttpResponse response;
            String responseString = "STRAWBERRIES";
            try {
                response = httpclient.execute(new HttpGet(uri[0]));
                StatusLine statusLine = response.getStatusLine();
                Log.d("info", statusLine.toString());
                if(statusLine.getStatusCode() == HttpStatus.SC_OK){
                    ByteArrayOutputStream out = new ByteArrayOutputStream();
                    response.getEntity().writeTo(out);
                    out.close();
                    responseString = out.toString();
                } else{
                    //Closes the connection.
                    response.getEntity().getContent().close();
                    throw new IOException(statusLine.getReasonPhrase());
                }
            } catch (ClientProtocolException e) {
               Log.d("OH CRIKEY", e.toString());
            } catch (IOException e) {
                Log.d("OH CRIKEY", e.toString());
            }
            Log.d("info", responseString);
            return responseString;
        }

        @Override
        protected void onPostExecute(String result) {

            //super.onPostExecute(result);

            try{
                List<Imagen> imgs = Parser.parseByDOM(result);
//Below prints 3280                
Log.d("UUYU", String.valueOf(imgs.get(0).getImagen().getRowBytes()));
//Below prints 550
                Log.d("UUYU2", String.valueOf(imgs.get(0).getImagen().getHeight()));
//Below prints 820
                Log.d("UUYU3", String.valueOf(imgs.get(0).getImagen().getWidth()));

                outputIv.setImageBitmap(imgs.get(0).getImagen());                   
                //vue.invalidate();

            }catch(Exception e){ e.printStackTrace();
            Log.d("ERROR!!", ""+e.getMessage());}
            //outputTv.setText(result);    
        }

        /**
         * @return the rootView
         */
        public View getRootView() {
            return rootView;
        }

        /**
         * @param rootView the rootView to set
         */
        public void setRootView(View rootView) {
            this.rootView = rootView;
        }
        }

位图具有正确的宽度和高度,并且似乎有数据。这是我解析 Web 服务响应字符串并设置位图的地方,只是为了完整性。所有的调试看起来都很好。

public static List<Imagen> parseByDOM(String response) throws ParserConfigurationException, SAXException, IOException{

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document doc = db.parse(new InputSource(new StringReader(response)));
    // normalize the document
    doc.getDocumentElement().normalize();
    // get the root node
    NodeList nodeList = doc.getElementsByTagName("return");
    Log.d("miaouw", nodeList.toString());
    List<Imagen> imgData = new ArrayList<Imagen>();

    for(int k = 0; k < nodeList.getLength(); k++){

        Node node=nodeList.item(0);
        Log.d("miaouw", node.getNodeName());

        Imagen img = new Imagen();

        try{

            for(int i = 0; i < node.getChildNodes().getLength(); i++){

                Node temp = node.getChildNodes().item(i);
                Log.d("miaouw", temp.getNodeName());

                if(temp.getNodeName().equalsIgnoreCase("fechaCreada")){

                    img.setFechaCreada(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss", Locale.ENGLISH).parse(temp.getTextContent()));
                }

                else if(temp.getNodeName().equalsIgnoreCase("imagen")){

                    String encodedImg = temp.getTextContent();
                    Log.d("vroom", encodedImg); //prints long, correct base64 binary String.  If I try to convert it to an image in http://www.freeformatter.com/base64-encoder.html, I get the correct image.
                    Log.d("toom ", String.valueOf(encodedImg.length())); //prints 19383

                    byte[] decodedString = Base64.decode(encodedImg, Base64.DEFAULT);
                    Log.d("woom", decodedString.toString()); //prints B@417b72f0
                    Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
                    img.setImagen(decodedByte);
                }

                else if(temp.getNodeName().equalsIgnoreCase("titulo")){

                    img.setTitulo(temp.getTextContent());
                }
            }

            imgData.add(img);
            Log.d("MIAUOW", img.toString()); //prints com.example.android.Imagen@4175ec58

            Log.d("noddy", node.toString());
            return imgData;

        }catch(ParseException e){

            Log.d("MEWL", e.getMessage());
        }
    }

    return null;
}

最后,这是我的布局。我将 ImageView 设置为默认的 Android drawable 只是为了确保视图正在更新(因为它是异步的)。当我单击测试时,src 图像消失了,但它没有被我的位图替换。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<Button
    android:id="@+id/button1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Test"
    android:onClick="onClickBtn" />
<ImageView
            android:id="@+id/imageView1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:contentDescription="yep"
            android:src="@drawable/ic_launcher"/>
</LinearLayout>

这意味着作为一个简单的概念证明,但它不起作用。我不明白位图是如何存在的,并且视图会更新,但位图是不可见的。

4

3 回答 3

0

在下面的代码中

else if(temp.getNodeName().equalsIgnoreCase("imagen")){

                String encodedImg = temp.getTextContent();
                Log.d("vroom", encodedImg); //prints long, correct base64 binary String.  If I try to convert it to an image in http://www.freeformatter.com/base64-encoder.html, I get the correct image.
                Log.d("toom ", String.valueOf(encodedImg.length())); //prints 19383

                byte[] decodedString = Base64.decode(encodedImg, Base64.DEFAULT);
                Log.d("woom", decodedString.toString()); //prints B@417b72f0
                Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
                img.setImagen(decodedByte);
            }

采用

img.setImageBitmap(decodedByte);

我不知道这是否是一个错字,或者你实际上已经输入了它,但如果它也在你的代码中,请替换它。

于 2013-08-31T22:07:44.120 回答
0
/**
 * @param outputIv the outputIv to set
 */
public void setOutputIv(ImageView outputIv) {
    this.outputIv = outputIv;
}

你用以下方式调用它:

setOutputIv((ImageView)findViewById(R.id.imageView1));

然后你把 this.outputIv 的引用放到 R.id.imageView1 中。您是否尝试过一次将类引用设置为 OnCreate() 中的 Layout.xml 中指定的类引用?

ImageView outputIv = (ImageView) findViewById(R.id.imageView1); 

我想否则你只是让 class.outputIv 有来自 R.id.imageView1 的内容,但是这个会显示它是 android:src="@drawable/ic_launcher" 独立于你添加到 class.outputIv 的内容甚至添加到 Activity 的视图中。

于 2013-08-31T22:08:45.467 回答
0

自我提醒:透明不等于白色。

我对 ImageView 做了以下操作:

outputIv.setBackgroundColor(Color.WHITE);

一切都很好。

于 2013-09-01T00:33:45.873 回答