Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6144929
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T18:43:17+00:00 2026-05-23T18:43:17+00:00

This is my first time posting, and after entering my title, I have gone

  • 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>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T18:43:18+00:00Added an answer on May 23, 2026 at 6:43 pm

    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();
            }
        }       
    }
    

    }

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

this is my first time posting here, I have a question which I have
this is my first time posting on here and have read alot of helpful
First time posting here after having great results searching for other answers on stackoverflow.
First time posting, please let me know if this question has already been answered!
This is my first time posting on Stackoverflow, but I've been reading through many
this is my first time posting a question here - I've searched for ones
first time posting here after having so many of my Google results come up
this is my first time posting on StackOverflow but this site is awesome. Thanks
use this website a lot but first time posting. My program creates a number
That's my first time posting on stackoverflow. I've been finding usefull answers on this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.