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>
Ensure you have the internet permission set in your manifest file
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:
}