3

Im trying to create a Bitmap from a Png file stored on the SD card and then set that Bitmap in an imageView. But its not working.

Here's the code:

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;

import com.pxr.tutorial.xmltest.R;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;
import android.widget.ImageView;

public class Getbackground {
    URL url;

    public static long downloadFile(URL url2) {
    try {

        URL url = new URL ("http://oranjelan.nl/oranjelan-bg.png");
        InputStream input = url.openStream();{
        try {

            File fileOnSD=Environment.getExternalStorageDirectory();    
            String storagePath = fileOnSD.getAbsolutePath();
            OutputStream output = new FileOutputStream (storagePath + "/oranjelanbg.png");
                try {

                    byte[] buffer = new byte[1024];
                    int bytesRead = 0;
                    while ((bytesRead = input.read(buffer, 0, buffer.length)) >= 0) {
                    output.write(buffer, 0, bytesRead);
                    }
                    } finally {
            output.flush();
            output.close();
//----------------------------------------------------------------------------------------------------------
            Bitmap BckGrnd = BitmapFactory.decodeFile(storagePath + "/oranjelanbg.png");
            ImageView BackGround=(ImageView)findViewById(R.id.imageView1);
            BackGround.setImageBitmap(BckGrnd);
//----------------------------------------------------------=-----------------------------------------------
            }
            } catch (IOException e) {
            throw new RuntimeException(e);
    } finally {
        try {
            input.close();
            } catch (IOException e) {
            throw new RuntimeException(e);
     }
   }    
 }    
        } catch (MalformedURLException ex) {
         throw new RuntimeException(ex);
    } catch (IOException e) {
      throw new RuntimeException(e);
    }

        return 0;
    }
//-----------------------------------------------------------------------------------------
    private static ImageView findViewById(int imageview1) {
        // TODO Auto-generated method stub
        return null;
    }
//-----------------------------------------------------------------------------------------
}

The File does load on the SD card succesfully but I cant seem to get the img in the view.

Michiel T
  • 537
  • 9
  • 23

1 Answers1

1

You can't..

ImageView BackGround=(ImageView)findViewById(R.id.imageView1);
BackGround.setImageBitmap(BckGrnd);

How can you get the reference of ImageView in non activity class Getbackground?

You can only update UI component in MainUI Thread and if its in non Activity class then using reference (Context) of that calling activity class only.

So put this code in your Activity class after Getbackground completes.

user370305
  • 108,599
  • 23
  • 164
  • 151
  • @user370305 you said __How can you get the reference of ImageView in non activity class Getbackground?__ but if we pass the context and the imageview reference to be updated to the `Getbackground` constructor, then it would be possible. am i right ? for lazyloading of images, we usually do like this. – sunil Jul 10 '12 at 11:09