Android 位图存在,但ImageView不显示它
在我的Android应用程序中,我正在调用一个Java web服务,它会返回一个图像Blob。我想在ImageView中将其显示为位图。此图像blob是HTML5画布元素的已保存toDataUrl()表示形式。主线程使用execute命令以onclick的形式启动AsyncTask内部类。以下是主线:Android 位图存在,但ImageView不显示它,android,web-services,android-asynctask,base64,android-imageview,Android,Web Services,Android Asynctask,Base64,Android Imageview,在我的Android应用程序中,我正在调用一个Java web服务,它会返回一个图像Blob。我想在ImageView中将其显示为位图。此图像blob是HTML5画布元素的已保存toDataUrl()表示形式。主线程使用execute命令以onclick的形式启动AsyncTask内部类。以下是主线: public class MainActivity extends Activity { private ImageView outputIv; p
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;
}
}
公共类MainActivity扩展活动
{
私有图像视图输出;
公众视野;
/**在首次创建活动时调用*/
@凌驾
创建时的公共void(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setOutputIv((ImageView)findViewById(R.id.imageView1));
}
公共void onClickBtn(视图五){
vue=v;
新建RequestTask(v)。执行(“http://10.0.2.2:8080/CasosDeUso5/webresources/casosdeusowsport/getimagenes?usuarioId=1");
}
/**
*@返回outputIv
*/
公共图像视图getOutputIv(){
返回输出;
}
/**
*@param outputIv要设置的输出
*/
public void setOutputIv(ImageView outputIv){
this.outputIv=outputIv;
}
类RequestTask扩展了AsyncTask{
私有视图rootView;
公共请求任务(视图根视图){
this.rootView=rootView;
}
@凌驾
受保护的字符串doInBackground(字符串…uri){
HttpClient HttpClient=新的DefaultHttpClient();
HttpResponse响应;
字符串responseString=“草莓”;
试一试{
response=httpclient.execute(新的HttpGet(uri[0]);
StatusLine StatusLine=response.getStatusLine();
Log.d(“info”,statusLine.toString());
if(statusLine.getStatusCode()==HttpStatus.SC\u OK){
ByteArrayOutputStream out=新建ByteArrayOutputStream();
response.getEntity().writeTo(out);
out.close();
responseString=out.toString();
}否则{
//关闭连接。
response.getEntity().getContent().close();
抛出新IOException(statusLine.getReasonPhrase());
}
}捕获(客户端协议例外e){
Log.d(“噢,CRIKEY”,例如toString());
}捕获(IOE异常){
Log.d(“噢,CRIKEY”,例如toString());
}
Log.d(“信息”,响应预算);
回报率;
}
@凌驾
受保护的void onPostExecute(字符串结果){
//super.onPostExecute(结果);
试一试{
List imgs=Parser.parseByDOM(结果);
//下面是3280
Log.d(“UUYU”,String.valueOf(imgs.get(0.getImagen().getRowBytes());
//下面是550
Log.d(“UUYU2”,String.valueOf(imgs.get(0.getImagen().getHeight());
//下面打印820
Log.d(“UUYU3”,String.valueOf(imgs.get(0.getImagen().getWidth());
setImageBitmap(imgs.get(0.getImagen());
//vue.invalidate();
}catch(异常e){e.printStackTrace();
Log.d(“错误!!”,“+e.getMessage());}
//输出v.setText(结果);
}
/**
*@返回根视图
*/
公共视图getRootView(){
返回rootView;
}
/**
*@param rootView要设置的rootView
*/
公共void setRootView(视图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;
}
publicstaticlist parseByDOM(字符串响应)抛出ParserConfigurationException、SAXException、IOException{
DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
DocumentBuilder db=dbf.newDocumentBuilder();
Document doc=db.parse(新的InputSource(新的StringReader(响应));
//规范文档
doc.getDocumentElement().normalize();
//获取根节点
NodeList NodeList=doc.getElementsByTagName(“返回”);
Log.d(“miaouw”,nodeList.toString());
List imgData=new ArrayList();
for(int k=0;k<?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>
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);
/**
* @param outputIv the outputIv to set
*/
public void setOutputIv(ImageView outputIv) {
this.outputIv = outputIv;
}
setOutputIv((ImageView)findViewById(R.id.imageView1));
ImageView outputIv = (ImageView) findViewById(R.id.imageView1);
outputIv.setBackgroundColor(Color.WHITE);