NRahman
NRahman

Reputation: 2757

Set the background of a View from a string URL

I want to set the background of a view from a string URL. I used this code to set an image from drawable:

The sample URL

View rightv = row.findViewById(R.id.four);
rightv.setBackgroundResource(R.drawable.demoimage);
rightv.setTag(position);
rightv.setOnClickListener(MembersDealActivity_a.this);

But when I tried to do the same from the URL, it did not work. I tried this sort of code too, but it did not work either:

Drawable res = getResources().getDrawable(imageResource);
right.setBackgroundDrawable(res);
right.setTag(position);
right.setOnClickListener(MembersDealActivity_a.this);

I also have tried to use the Picasso library, but when it request to set a value into parameter view it can't be cast there.

Picasso.with(this)
    .load(imag_link).resize(600, 350)
    .into(imv);

The full code:

public class MyActivity extends Activity implements
        AdapterView.OnItemClickListener, View.OnClickListener {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ListView list = new ListView(this);
        setContentView(list);

        String[] items = { "Tom", "Sally", "Bill", "John", "Santiago",
                "Isabella" };

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                R.layout.review, R.id.textView1, items) {
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                View row = super.getView(position, convertView, parent);


                View text = row.findViewById(R.id.seemore);
                text.setTag(position);
                text.setOnClickListener(MyActivity.this);



                View left = row.findViewById(R.id.imageView1);
                left.setBackgroundResource(R.drawable.newapture);
                left.setTag(position);
                left.setOnClickListener(MyActivity.this);


                return row;
            }
        };

Upvotes: 1

Views: 12968

Answers (6)

Nisha Jain
Nisha Jain

Reputation: 757

For set image as wallpaper you can use this..

wallpaperManager=WallpaperManager.getInstance(getApplicationContext());
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
            .permitAll().build();
StrictMode.setThreadPolicy(policy);

// Button Onclick

 setWallpaper.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
            InputStream ins = null;
            try {
                  ins = new URL(url).openStream();
                  wallpaperManager.setStream(ins);
            } catch (IOException e) {
                  e.printStackTrace();
            }
            FancyToast.makeText(getApplicationContext(), "Set Successfully", 
            FancyToast.LENGTH_SHORT, FancyToast.SUCCESS, false).show();
        }
     });

Please add the permissions in AndroidManifest.xml of Set_Wallpaper and Internet.

Upvotes: 0

Zak Chapman
Zak Chapman

Reputation: 355

i`m using a class for load Image into ImageView from URL:

Here: 1. create a class ImageFromUrlUtil.java

package com.ntjp.halalnavi.guide.util;

import android.graphics.Bitmap.Config;
import android.widget.ImageView;

import com.ntjp.halalnavi.guide.R;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;

public class ImageFromUrlUtil {

    public static void displayImage(ImageView view, String path, ImageLoadingListener listener) {
        ImageLoader loader = ImageLoader.getInstance();
        try {
            loader.displayImage(path, view, DEFAULT_DISPLAY_IMAGE_OPTIONS, listener);
        } catch (OutOfMemoryError e) {
            e.printStackTrace();
            loader.clearMemoryCache();
        }
    }

    public static void displayRoundImage(ImageView view, String path, ImageLoadingListener listener) {
        ImageLoader loader = ImageLoader.getInstance();
        try {
            loader.displayImage(path, view, ROUND_DISPLAY_IMAGE_OPTIONS, listener);
        } catch (OutOfMemoryError e) {
            e.printStackTrace();
            loader.clearMemoryCache();
        }
    }

    public static void loadImage(String path, ImageLoadingListener listener) {
        ImageLoader loader = ImageLoader.getInstance();
        try {
            loader.loadImage(path, DEFAULT_DISPLAY_IMAGE_OPTIONS, listener);
        } catch (OutOfMemoryError e) {
            e.printStackTrace();
        }
    }

    //TODO Change default image
    private static final DisplayImageOptions.Builder DEFAULT_DISPLAY_IMAGE_OPTIONS_BUIDLER = new DisplayImageOptions.Builder()
            .imageScaleType(ImageScaleType.IN_SAMPLE_POWER_OF_2)
            .displayer(new FadeInBitmapDisplayer(300, true, false, false))
            .showImageForEmptyUri(R.drawable.default_image)
            .showImageOnLoading(R.drawable.default_image)
            .showImageOnFail(R.drawable.default_image).cacheOnDisk(true)
            .cacheInMemory(true).bitmapConfig(Config.ARGB_8888);

    private static final DisplayImageOptions DEFAULT_DISPLAY_IMAGE_OPTIONS = DEFAULT_DISPLAY_IMAGE_OPTIONS_BUIDLER
            .build();
    private static final DisplayImageOptions ROUND_DISPLAY_IMAGE_OPTIONS = DEFAULT_DISPLAY_IMAGE_OPTIONS_BUIDLER
            .displayer(new RoundedBitmapDisplayer(500)).build();
}

then call this method from anywhere you want:

for a rounded Image us:

ImageUtil.displayRoundImage(avt, avtUrl, null);

for a normal Image us:

ImageUtil.displayImage(thumb, avtUrl, null);

Finaly add this Library to your project: Link

Upvotes: 0

Sunil Kumar
Sunil Kumar

Reputation: 7082

you can try this one

Bitmap bitmap = BitmapFactory.decodeStream(imageUrl.openConnection().getInputStream());
BitmapDrawable background = new BitmapDrawable(getResources(), bitmap);
rightv.setBackgroundDrawable(background);

Upvotes: 1

Bhavin Nattar
Bhavin Nattar

Reputation: 3215

Use This code may this help you...

URL url = new URL(Your URL);
Bitmap bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream()); imageView.setImageBitmap(bmp);

Or refer these link Click Here

Upvotes: 0

ʌɐɥqıɐʌ
ʌɐɥqıɐʌ

Reputation: 147

try this..URL url = new URL(Your Url); Bitmap bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream()); imageView.setImageBitmap(bmp);

Upvotes: 1

Faiz Anwar
Faiz Anwar

Reputation: 658

Set the background of a view inside Async task because you are getting image from url. Try it I hope it will help you.

Upvotes: 2

Related Questions