Ronnie
Ronnie

Reputation: 11198

Load image from URL

I've searched and tried a couple different methods. I am coming up short on both. This is my current method:

package com.dop.mobilevforum;

import java.io.InputStream;
import java.net.URL;

import android.app.Activity;
import android.content.Context;
import android.content.res.Configuration;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;
import android.widget.VideoView;

public class Vforum extends Activity
{
    private String imgPath = "http://mysite.com/mv/vfdemo1/slides/slide1.jpg";

    private ImageView slideHolder;

    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.vforum);

        slideHolder = (ImageView) findViewById(R.id.slideHolder);

        Drawable drawable = LoadImageFromWebOperations(imgPath);
        slideHolder.setImageDrawable(drawable);
    }

    private Drawable LoadImageFromWebOperations(String url)
    {
        try
        {
            InputStream is = (InputStream) new URL(url).getContent();
            Drawable d = Drawable.createFromStream(is, "src name");
            return d;
        } 
        catch (Exception e)
        {
            Log.w("LoadImageFromWebOperations",e.toString());
            return null;
        }
    }
}

the LoadImageFromWebOperations method is returning a bitmap, so I know that part is working. It is the slideHolder.setImageDrawable(drawable); that I think it failing. This is my XML:

<?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/mobile_vforum_bg">
    <ImageView 
        android:id="@+id/slideHolder"
        android:layout_width="320px" 
        android:layout_height="240px">
    </ImageView>
</FrameLayout>

any ideas? I just get a black screen, no errors.

Upvotes: 1

Views: 5682

Answers (2)

George
George

Reputation: 1327

Your code work, my dear friend your url don't work http://mysite.com/mv/vfdemo1/slides/slide1.jpg.

You can use this it works 100%, but your code also works

URL url = new URL("http://variable3.com/files/images/email-sig.jpg");
Bitmap bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
slideHolder.setImageBitmap(bmp);

Upvotes: 0

Gligor
Gligor

Reputation: 2940

Your best bet is using a WebView, but if you really want to do it this way don't use setImageDrawable. You should use setImageBitmap. If your LoadImageFromWebOperations returns a Bitmap then you shouldn't change anything, but the function you are using and it should work smoothly.

Upvotes: 2

Related Questions