change background image of Framelayout via URL
Asked Answered
I

4

4

is it possible to use a url to set background for framelayout?

Example: i want to use the URL like this as my framelayout background.

what i really want to achieve is that when my activity loads it will fetch the image from my domain and use it as my framelayout background image. i was able to do this but i used a imageview and a button.

below is the code that i use:

public class MyAppActivity extends Activity {

ImageView imView;
    String imageUrl= "http://www.mydomain.com/resource/images/";

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

setContentView(R.layout.main);
Button bt3= (Button)findViewById(R.id.postbutton);
bt3.setOnClickListener(getImgListener);

}
View.OnClickListener getImgListener = new View.OnClickListener()
{

      public void onClick(View view) {

           downloadFile(imageUrl+"image"+".png");
           Log.i("im url",imageUrl+"image"+".png");
      }

};


Bitmap bmImg;
void downloadFile(String fileUrl){
      URL myFileUrl =null;          
      try {
           myFileUrl= new URL(fileUrl);
      } catch (MalformedURLException e) {
           // TODO Auto-generated catch block
           e.printStackTrace();
      }
      try {
           HttpURLConnection conn= (HttpURLConnection)myFileUrl.openConnection();
           conn.setDoInput(true);
           conn.connect();
           InputStream is = conn.getInputStream();

           bmImg = BitmapFactory.decodeStream(is);
           imView.setImageBitmap(bmImg);
      } catch (IOException e) {
           // TODO Auto-generated catch block
           e.printStackTrace();
      }
 }

 }

and here is my XML codes:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/rakistaradiobg"
android:orientation="vertical" >

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="98dp"
    android:layout_gravity="bottom|center"
    android:scaleType="fitXY"
    android:src="@drawable/btnbg" />

<Button
    android:id="@+id/postbutton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="50dp"
    android:text="Button" />

Insusceptible answered 21/12, 2011 at 6:53 Comment(0)
S
4

Something interested, (I never try this but it should work)

FrameLayout fm = (FrameLayout)findViewById(R.id.FrameLayout01);      
Drawable drw = ImageOperations(this,url,filename)
fm.setBackgroundDrawable(drw)

Convert your image url in Drawable using

private Drawable ImageOperations(Context ctx, String url, String saveFilename) {
        try {
            InputStream is = (InputStream) this.fetch(url);
            Drawable d = Drawable.createFromStream(is, saveFilename);
            return d;
        } catch (MalformedURLException e) {
            e.printStackTrace();
            return null;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    public Object fetch(String address) throws MalformedURLException,IOException {
        URL url = new URL(address);
        Object content = url.getContent();
        return content;
    }

Try this and let me know what happen..

Soulless answered 21/12, 2011 at 7:7 Comment(4)
it gives me an error it says: SKImageDecoder: :Factory Returned null what thats mean??Insusceptible
java.sogeti.nl/JavaBlog/wp-content/uploads/2009/04/… -- this link is works fine..Soulless
setBackgroundDrawable() is deprecated in API 16 .how to set image as a backgroung of FrameLayoutPitiable
@Hemant - You can still use it as there is backward compatibility is available.Soulless
T
0

You may to use WebView to display web-site content. There is also wonderful tutorial of using WebView on android developers web site

Tsar answered 21/12, 2011 at 7:6 Comment(0)
F
0

here is the code by you can get image in bitmap from URL

 Bitmap bitmap = BitmapFactory.decodeStream((InputStream)new URL(imageUrl).getContent());

and Useing This you can convert bitmap to drawable ...

and after getting drawable apply in layout like

framelyt.setBackgroundDrawable(d);
Focalize answered 21/12, 2011 at 7:8 Comment(0)
E
0
private RelativeLayout myRelativeLauout;

myRelativeLauout = (RelativeLayout)findViewById(R.id.rlBg);  
new LoadBackground("http://www.tmonews.com/wp-content/uploads/2012/10/androidfigure.jpg",
            "androidfigure").execute();


private class LoadBackground extends AsyncTask<String, Void, Drawable> {

    private String imageUrl , imageName;

    public LoadBackground(String url, String file_name) {
        this.imageUrl = url;
        this.imageName = file_name;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected Drawable doInBackground(String... urls) {

        try {
            InputStream is = (InputStream) this.fetch(this.imageUrl);
            Drawable d = Drawable.createFromStream(is, this.imageName);
            return d;
        } catch (MalformedURLException e) {
            e.printStackTrace();
            return null;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
    private Object fetch(String address) throws MalformedURLException,IOException {
        URL url = new URL(address);
        Object content = url.getContent();
        return content;
    }


    @Override
    protected void onPostExecute(Drawable result) {
        super.onPostExecute(result);
        myRelativeLauout.setBackgroundDrawable(result);
    }
}
Emeraldemerge answered 2/5, 2014 at 5:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.