0

This is my first time posting, and after entering my title, I have gone through every relevant question and am still having troubles. Plain and simple, person enters image url into and EditText, they hit a button, the image appears under the button. I am using BitmapFactory and need to keep it that way. I also have the AndroidManifest.xml already includes the appropriate INTERNET permission.

The Java is as so:

package com.display.picture;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;

import android.app.Activity;
import android.os.Bundle;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

public class DisplayPictureActivity extends Activity {

    private EditText et;
    private Button btn;
    private ImageView iv;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        et = (EditText) findViewById(R.id.et1);
        iv = (ImageView) findViewById(R.id.iv1);
        btn = (Button) findViewById(R.id.btn1);

        btn.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                try
                {
                    URL url = new URL(et.getText().toString());
                    URLConnection connection = url.openConnection();
                    connection.connect();
                    InputStream is = connection.getInputStream();
                    BufferedInputStream bis = new BufferedInputStream(is);
                    Bitmap bitmap = BitmapFactory.decodeStream(bis);
                    ImageView myImage = (ImageView) findViewById(R.id.iv1);
                    myImage.setImageBitmap(bitmap);
                    bis.close();
                    is.close();
                }
                catch (Exception e) {
                    Toast toast = Toast.makeText(getBaseContext(), "Failed to Download Image", Toast.LENGTH_LONG);
                    toast.show();
                }
                Bitmap image = null;
                iv.setImageBitmap(image);
            }
        });
    }
}

The majority of the questions I see here involve having hard-coded hyperlinks. While here I'm supposed to allow the user to enter an image url and then pass that through the Bitmap, and display it within an imageview after hitting a button

My manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.display.picture"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="8" />
    <uses-permission android:name="android.permission.INTERNET"></uses-permission>

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".DisplayPictureActivity"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>
Greg
  • 5
  • 6
  • Can you describe the problem you are having? – Rob Jul 15 '11 at 03:12
  • Oh, I've tried different image URLs and it's not displaying the image. At least my toast works, telling me it failed to download the image. – Greg Jul 15 '11 at 03:13
  • Blocking the UI while your image downloads is probably not a good choice – Thomas Dignan Jul 15 '11 at 03:32
  • I don't know what you mean. I'm a super novice developer. I got rid of setting it to a null value. User enters image url Hits "Display Image" button Image gets displayed on the bottom of the same window. – Greg Jul 15 '11 at 03:35

1 Answers1

1

Ensure you have the internet permission set in your manifest file

<uses-permission android:name="android.permission.INTERNET"/>

You are attempting to close both the input stream and the buffered input stream that uses the input stream, removing one of those closes will fix the exception thrown.

You may also want to use an AsyncTask to perform the download, as blocking the UI while the download occurs is generally not a good idea.

Here is your code modified to use an AsyncTask to download:

public class DisplayPictureActivity extends Activity {
private Button btn;
private EditText et;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    btn = (Button)findViewById(R.id.button1);
    et = (EditText)findViewById(R.id.editText1);

    btn.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            try {
                URL url = new URL(et.getText().toString());
                //URL url = new URL("http://farm7.static.flickr.com/6138/5935946400_934994190e_s_d.jpg");
                new MyDownloadTask().execute(url);                                  
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

private class MyDownloadTask extends AsyncTask<URL, Integer, Bitmap> {
    @Override
    protected Bitmap doInBackground(URL... params) {
        URL url = params[0];
        Bitmap bitmap = null;
        try {
            URLConnection connection = url.openConnection();
            connection.connect();
            InputStream is = connection.getInputStream();
            BufferedInputStream bis = new BufferedInputStream(is);
            bitmap = BitmapFactory.decodeStream(bis);
            bis.close();
            //is.close(); THIS IS THE BROKEN LINE
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

        return bitmap;
    }

    @Override
    protected void onPostExecute(Bitmap bitmap) {
        if (bitmap != null) {
            ImageView myImage = (ImageView) findViewById(R.id.imageView1);
            myImage.setImageBitmap(bitmap);
        } else {
            Toast.makeText(getApplicationContext(), "Failed to Download Image", Toast.LENGTH_LONG).show();
        }
    }       
}

}

Rob
  • 4,733
  • 3
  • 32
  • 29
  • Rob, I have already included that permission as stated my original post. I don't understand what blocking the UI means and what is AsyncTask? – Greg Jul 15 '11 at 03:37
  • Can you add your manifest file to your original question. I have run your code and it works correctly. There are 2 potentials problems you have: 1) Your internet permission is in the wrong place in the manifest 2) You are using an emulator and have not set up the emulators proxy correctly – Rob Jul 15 '11 at 03:38
  • Your manifest looks ok. To check if your proxy is ok can you open up the android browser on the device/emulator and see if the browser can load the image url? – Rob Jul 15 '11 at 03:45
  • Oh I have checked the emulators browser and have viewed images in it as well. Trying the method I would use if this were on an actual device. Long pressing, hitting view image, then long pressing and copying the url. – Greg Jul 15 '11 at 03:46
  • I've updated answer with example of using AsyncTask to do the download. Can you post more details on what the exception is you are getting? As your code is running ok on my device – Rob Jul 15 '11 at 04:02
  • The problem is that after entering an image URL (I have tested several) and hitting Display Image, my toast pops up saying it failed to download the image and no image gets displayed. I already turned in this assignment with fear that I may not be able to submit it after the deadline. I'll try this to see if this works at least. – Greg Jul 15 '11 at 04:04
  • Ok found the bug, it was the is.close(). That was closing the input stream a second time and causing an exception. Remove that line and it run for you – Rob Jul 15 '11 at 04:06
  • I ran the code you provided (before removing the is.close()) and it ran. Going to run it again without the is.close and see what difference it makes. Maybe I was using bad image urls EDIT: Running your code again without the is.close() gave me the same error I had before. – Greg Jul 15 '11 at 04:09