-3

I use many ways of external imageview from url. but all the time activity stopped unfortunately. Do you know thats why. I have internet perimision in manifest file. But I could not solve it. is anyone can help me. ?

I use this ways in the link. but not working in my project. how to display external image in android?

        public class MainActivity extends Activity{
            ImageView imgView;
            EditText inputUrl;
            OnClickListener getImageBtnOnClick = new OnClickListener() {
                public void onClick(View view) {
                    Context context = view.getContext();
                    Editable ed = inputUrl.getText();
                    Drawable image = ImageOperations(context,"http://www.cssnz.org/flower.jpg" ,"image.jpg");

                    imgView = (ImageView)findViewById(R.id.i1);
                    imgView.setImageDrawable(image);
                }
            };

            public void onCreate(Bundle icicle) {
                super.onCreate(icicle);
                setContentView(R.layout.activity_main);
                inputUrl = ((EditText)findViewById(R.id.imageURL));
                inputUrl.setSingleLine();
                inputUrl.setTextSize(11);
                Button getImageButton = (Button)findViewById(R.id.getImageButton);
                getImageButton.setOnClickListener(getImageBtnOnClick);

            }   

            private Drawable ImageOperations(Context ctx, String url, String saveFilename) {
                try {
                    InputStream is = (InputStream) this.fetch(url);
                    Drawable d = Drawable.createFromStream(is, "src");
                    return d;
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                    return null;
                } catch (IOException e) {
                    e.printStackTrace();
                    return null;
                }
            }

            public Object fetch(String address) throws MalformedURLException,IOException {
                URL url = new URL(address);
                Object content = url.getContent();
                return content;
            }
        }
    xml file
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <Button
            android:id="@+id/getImageButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="39dp"
            android:layout_marginTop="22dp" />

        <ImageView
            android:id="@+id/i1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="106dp"
            android:src="@drawable/ic_action_search" />

        <EditText
            android:id="@+id/imageURL"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ems="10" >

            <requestFocus />
        </EditText>

    </LinearLayout>


    android.manifest  

    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.example.externalimageview"
        android:versionCode="1"
        android:versionName="1.0" >

        <uses-sdk
            android:minSdkVersion="8"
            android:targetSdkVersion="15" />
        <uses-permission android:name="android.permission.INTERNET"/>

        <application
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
            <activity
                android:name=".MainActivity"
                android:label="@string/title_activity_main" >
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />

                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
        </application>

    </manifest>

log cat
09-14 19:56:31.409: W/dalvikvm(1097): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
09-14 19:56:31.459: E/AndroidRuntime(1097): FATAL EXCEPTION: main
09-14 19:56:31.459: E/AndroidRuntime(1097): android.os.NetworkOnMainThreadException
09-14 19:56:31.459: E/AndroidRuntime(1097):     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
09-14 19:56:31.459: E/AndroidRuntime(1097):     at java.net.InetAddress.lookupHostByName(InetAddress.java:391)
09-14 19:56:31.459: E/AndroidRuntime(1097):     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:242)
09-14 19:56:31.459: E/AndroidRuntime(1097):     at java.net.InetAddress.getAllByName(InetAddress.java:220)
09-14 19:56:31.459: E/AndroidRuntime(1097):     at libcore.net.http.HttpConnection.<init>(HttpConnection.java:71)
09-14 19:56:31.459: E/AndroidRuntime(1097):     at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
09-14 19:56:31.459: E/AndroidRuntime(1097):     at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:351)
09-14 19:56:31.459: E/AndroidRuntime(1097):     at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:86)
09-14 19:56:31.459: E/AndroidRuntime(1097):     at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
09-14 19:56:31.459: E/AndroidRuntime(1097):     at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:308)
09-14 19:56:31.459: E/AndroidRuntime(1097):     at libcore.net.http.HttpEngine.connect(HttpEngine.java:303)
09-14 19:56:31.459: E/AndroidRuntime(1097):     at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:282)
09-14 19:56:31.459: E/AndroidRuntime(1097):     at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:232)
09-14 19:56:31.459: E/AndroidRuntime(1097):     at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:80)
09-14 19:56:31.459: E/AndroidRuntime(1097):     at java.net.URLConnection.getContent(URLConnection.java:194)
09-14 19:56:31.459: E/AndroidRuntime(1097):     at java.net.URL.getContent(URL.java:447)
09-14 19:56:31.459: E/AndroidRuntime(1097):     at com.example.externalimageview.MainActivity.fetch(MainActivity.java:61)
09-14 19:56:31.459: E/AndroidRuntime(1097):     at com.example.externalimageview.MainActivity.ImageOperations(MainActivity.java:47)
09-14 19:56:31.459: E/AndroidRuntime(1097):     at com.example.externalimageview.MainActivity.access$0(MainActivity.java:45)
09-14 19:56:31.459: E/AndroidRuntime(1097):     at com.example.externalimageview.MainActivity$1.onClick(MainActivity.java:27)
09-14 19:56:31.459: E/AndroidRuntime(1097):     at android.view.View.performClick(View.java:3511)
09-14 19:56:31.459: E/AndroidRuntime(1097):     at android.view.View$PerformClick.run(View.java:14105)
09-14 19:56:31.459: E/AndroidRuntime(1097):     at android.os.Handler.handleCallback(Handler.java:605)
09-14 19:56:31.459: E/AndroidRuntime(1097):     at android.os.Handler.dispatchMessage(Handler.java:92)
09-14 19:56:31.459: E/AndroidRuntime(1097):     at android.os.Looper.loop(Looper.java:137)
09-14 19:56:31.459: E/AndroidRuntime(1097):     at android.app.ActivityThread.main(ActivityThread.java:4424)
09-14 19:56:31.459: E/AndroidRuntime(1097):     at java.lang.reflect.Method.invokeNative(Native Method)
09-14 19:56:31.459: E/AndroidRuntime(1097):     at java.lang.reflect.Method.invoke(Method.java:511)
09-14 19:56:31.459: E/AndroidRuntime(1097):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
09-14 19:56:31.459: E/AndroidRuntime(1097):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
09-14 19:56:31.459: E/AndroidRuntime(1097):     at dalvik.system.NativeStart.main(Native Method)
Community
  • 1
  • 1
slhngndz
  • 11
  • 2
  • 5
    Post the *actual* code you're using, and the *actual* error log. "I used this tutorial and it crashes" gives us *nothing* to work with. – Cat Sep 14 '12 at 18:25
  • I add codes this the solution I tried last. I tried many ways can not have result.thanks you. – slhngndz Sep 14 '12 at 19:06
  • You've still not said what the error is, nor have you showed any effort to solve this yourself. – Cat Sep 14 '12 at 19:53
  • I send you the log cat. thanks. if I click the button it says activity has stopped unfortunately. sorry for bad explain. this is my second post in stackoverflow. thank you very much. – slhngndz Sep 14 '12 at 20:05
  • I think I must use async task – slhngndz Sep 14 '12 at 20:22

2 Answers2

0

You are facing android.os.NetworkOnMainThreadException. This exception has been newly introduced to limit your app from accessing network from the main UI thread.

The main use of this is to avoid your app from stop responding to user Interactions. When a network is accessed from the main thread, it actually blocks the UI from responding until the methods have been finished executing. So in order to avoid this android throws you this exception.

So what you probably have to do is, make use of a Worker Thread or an AsyncTask.

Andro Selva
  • 53,910
  • 52
  • 193
  • 240
0

Please Use Async Task for download image or you can use below code for solve this problem, write below code into your MainActivity file after setContentView(R.layout.activity_main);

if (android.os.Build.VERSION.SDK_INT > 9) {
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);
}
Dipak Keshariya
  • 22,193
  • 18
  • 76
  • 128