下载的图像用作我的framelayout中的背景图像

时间:2011-12-22 03:32:57

标签: android android-layout background-image drawable android-framelayout

我有一个下载的文件,我想在我的framelayout中用作背景图片。我使用这段代码,但它给了我一个错误。

这是我收到错误的行:

fm.setBackgroundDrawable(dr);

,这是我的整个代码

public class MyAppActivity extends Activity {
private final String PATH = "/data/data/com.myapp/";  //put the downloaded file here
private Drawable imgsrc;
private FrameLayout fm;

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.main);
FrameLayout fm = (FrameLayout)findViewById(R.id.framelyt);      
//Drawable drw = ImageOperations(this,url,filename)
Button btn = (Button)findViewById(R.id.postbutton);
btn.setOnClickListener(test);
 }  

View.OnClickListener test = new View.OnClickListener() {

    public void onClick(View v) {
        // TODO Auto-generated method stub
    DownloadFromUrl("http://www.mydomain.com/radio/images/", "image.png");


    }
};

private void DownloadFromUrl(String imageURL, String fileName) {
    // TODO Auto-generated method stub
     try {
         URL url = new URL(imageURL + fileName); //you can write here any link
         File file = new File(PATH + fileName);

         if(file.exists())
         {
             file.delete();
         }

         long startTime = System.currentTimeMillis();
         Log.d("ImageManager", "download begining");
         Log.d("ImageManager", "download url:" + url);
         Log.d("ImageManager", "downloaded file name:" + fileName);

         /* Open a connection to that URL. */
         URLConnection ucon = url.openConnection();

         /*
          * Define InputStreams to read from the URLConnection.
          */
         InputStream is = ucon.getInputStream();
         BufferedInputStream bis = new BufferedInputStream(is);

         /*
          * Read bytes to the Buffer until there is nothing more to read(-1).
          */
         ByteArrayBuffer baf = new ByteArrayBuffer(50);
         int current = 0;
         while ((current = bis.read()) != -1) {
                 baf.append((byte) current);
         }

         /* Convert the Bytes read to a String. */
         FileOutputStream fos = new FileOutputStream(file);
         fos.write(baf.toByteArray());

         Bitmap bMap = BitmapFactory.decodeFile(PATH + fileName);
         BitmapDrawable dr = new BitmapDrawable(bMap);
        // Drawable d = new BitmapDrawable();
         fm.setBackgroundDrawable(dr);
         fos.close();
         Log.d("ImageManager", "download ready in "
                         + ((System.currentTimeMillis() - startTime) / 1000)
                         + " sec");

         //fm.setBackgroundDrawable(d);

 } catch (IOException e) {
         Log.d("ImageManager", "Error: " + e);
 }
}

}

0 个答案:

没有答案