2

我与我的Android应用程序代码挣扎......Android的下载图像通过intentservice OK,但不能将其设置在ImageView的

我试图下载一个图片(通过intentService),然后以显示它将一个ImageView添加到LinearLayout。

到目前为止,我已经通过管理下载的intentservice向我发送了绝对路径。我得到路径名称的祝酒。

但我无法通过添加ImageView的显示画面......我真的不知道为什么......

起初,我以为我的处理程序无法达到布局,但我设法使用setImageResource显示我的资源的随机图像。所以我想问题就来了无论是从setimagebitmap或bitmapfactory解码...

在我Intentservice我有(输出为我的文件的名称):

Messenger messenger = (Messenger) extras.get("MESSENGER"); 
Message msg = Message.obtain(); 
msg.arg1 = result; 
msg.obj = output.getAbsolutePath(); 
try { 
    messenger.send(msg); 
} etc... 

回到我的主要活动,我有:

Private Handler handler = new Handler() { 
    public void handleMessage(Message message) { 

     // I Get the message 
     Object path = message.obj; 

     //Check if download went ok 
     if (message.arg1 == RESULT_OK && path != null) { 
     Toast.makeText(Update3sur3.this, 
      "Downloaded" + path.toString(), Toast.LENGTH_LONG) 
      .show(); 

      //Try to display the pic 
      LinearLayout res2=(LinearLayout)findViewById(R.id.reslayout2); 
     ImageView imgView2 = new ImageView(Update3sur3.this); 
     Bitmap bitmap= BitmapFactory.decodeFile(path.toString()); 
     imgView2.setImageBitmap(bitmap); 
     res2.addView(imgView2); 
    } else { 
     Toast.makeText(Update3sur3.this, "Download failed.", 
      Toast.LENGTH_LONG).show(); 
     } 
    }; 
    }; 

非常感谢!

洛朗

+0

您面临的问题是什么? 是强制关闭还是图像不显示? – 2013-02-20 22:13:57

回答

1

起初我会尽量确保如果我能正确即访问该文件,请按照下列步骤操作:

  1. 使用decodeByteArray(字节[]的数据,诠释抵消,诠释长度)方法而不是BitmapFactory.decodeFile(path.toString())

  2. 要使用这种新方法,你必须自己读取文件的字节数组,这将会增加额外的步骤,但是你可以确定你正在正确读取文件。

  3. 使用的FileInputStream要读取的字节

文件镜像文件=新的文件(路径); byte imageData [] = new byte [(int)imageFile.length()];

的BufferedInputStream在新= BufferedInputStream为(新的FileInputStream(镜像文件);

in.read(的imageData,0,(INT)imageFile.length());

Log.d( “”,“图像文件大小:“+ imageFile.length()); //在这里检查文件大小是否正确?

//现在imageData是您将用来生成图像的字节数组: decodeByteArray(imageData ,0,(int)imageFile.length());

如果图像仍然没有出现,那么把日志报告和打印图像的大小,看看它是否正确。

+0

它与FileInputStream一起工作。谢谢。 – 2013-02-21 04:00:19

+0

欢迎您。请接受答案。 – 2013-02-21 09:13:53

+0

@LaurentMilleron我很高兴它解决了你的问题。如果你发现有用的东西然后upvote,如果它已经解决了你的问题,然后接受答案。 – 2013-02-21 09:22:42

相关问题