下载的图像用作我的框架布局中的背景图像

发布于 2024-12-22 13:13:57 字数 2744 浏览 2 评论 0原文

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

这是我收到错误的行:

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);
 }
}

}

I have a downloaded file and i want to use as background image in my framelayout. I use this code but it gives me an error .

here is the line where i get the error:

fm.setBackgroundDrawable(dr);

and here is my whole code

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);
 }
}

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文