1

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

}
tskuzzy
  • 35,812
  • 14
  • 73
  • 140
HeartlessArchangel
  • 1,737
  • 4
  • 13
  • 17
  • what error you are getting post the logcat. – Lalit Poptani Dec 22 '11 at 04:30
  • im am getting a null pointer from the line: fm.setBackgroundDrawable(dr); heres the screenhot: https://lh4.googleusercontent.com/-wq0UKie3TDI/TvK19Z8gk7I/AAAAAAAAAJM/21xNS0rHgy4/w1058-h254-k/untitled.JPG – HeartlessArchangel Dec 22 '11 at 04:42
  • Then either your fm is null or it means your image is not getting downloaded check your code for downloading an image. – Lalit Poptani Dec 22 '11 at 04:44
  • i am sure that the image is being downloaded here is the screen shot: https://lh6.googleusercontent.com/-mCjjn7z2o00/TvK2u6OktPI/AAAAAAAAAJc/yN8-rOkcqpo/w615-h198-k/untitled1.JPG – HeartlessArchangel Dec 22 '11 at 04:49
  • double click on the link after null pointer exception will give you the link where you are getting exception. – Lalit Poptani Dec 22 '11 at 04:56
  • my FrameLayout is not null i think i have a this fm = (FrameLayout)findViewById(R.id.framelyt); on top my oncreate method. i did double click on the link in logcat and it directs me to fm.setBackgroundDrawable(dr); – HeartlessArchangel Dec 22 '11 at 04:57
  • What error is this giving you? – Chris Dec 22 '11 at 03:43
  • here is the error that i get in logcat: https://lh4.googleusercontent.com/-wq0UKie3TDI/TvK19Z8gk7I/AAAAAAAAAJM/21xNS0rHgy4/w1058-h254-k/untitled.JPG – HeartlessArchangel Dec 22 '11 at 04:46
  • try this fm.setBackgroundResource(R.id.icon); does it give error check it. – Lalit Poptani Dec 22 '11 at 05:01
  • i dont have R.id.icon in my R.java, however i did try setting R.drawable.image and it works perfectly but thats not the image i want to use. im getting my image from the file that i have downloaded earlier and it is save in /data/data/myapp/files/ i can see it in my ddms and thats the image that i want to use in my framelayout background – HeartlessArchangel Dec 22 '11 at 05:08
  • i got it but i cant post my solution i am lack of reputation to answer my own question. but here is the code that i've use. Bitmap bMap = BitmapFactory.decodeFile(PATH + fileName); BitmapDrawable dr = new BitmapDrawable(bMap); imgsrc = new File(getFilesDir(),"radioad.jpg").getAbsolutePath(); //Drawable d = new BitmapDrawable(imgsrc); //imgview.setImageDrawable(d); Drawable x = Drawable.createFromPath(imgsrc); fm.setBackgroundDrawable(x); fos.close(); – HeartlessArchangel Dec 22 '11 at 05:25

0 Answers0