0

在这里,我尝试从 Url Connection 检索图像,但我无法获取,请帮助解决此问题

HttpDownloadActivity.java 编码

包 com.HttpDownload;

import android.app.Activity;
import android.os.Bundle;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class HttpDownloadActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        System.out.print("HI.....");
        Bitmap bitmap =
                DownloadImage(
                "http://bestpropertyworld.com/photos/blue-fish.jpg");
       ImageView img = (ImageView) findViewById(R.id.img);
                img.setImageBitmap(bitmap);
            /*  String str =
                        DownloadText("http://www.appleinsider.com/appleinsider.rss");
                        txt = (TextView) findViewById(R.id.text);
                        txt.setText(str);*/
    }

private Bitmap DownloadImage(String URL)
{
    System.out.print("Bitmap DownloadImage");
Bitmap bitmap = null;
InputStream in = null;
try {
in = OpenHttpConnection(URL);
bitmap = BitmapFactory.decodeStream(in);
in.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
return bitmap;
}
    private InputStream OpenHttpConnection(String urlString)
            throws IOException
            {
        System.out.print("InputStream OpenHttpConnection");
            InputStream in = null;
            int response = -1;
            URL url = new URL(urlString);
            URLConnection conn = url.openConnection();
            if (!(conn instanceof HttpURLConnection))
            throw new IOException("Not an HTTP connection");
            try{
            HttpURLConnection httpConn = (HttpURLConnection) conn;
            httpConn.setAllowUserInteraction(false);
            httpConn.setInstanceFollowRedirects(true);
            httpConn.setRequestMethod("GET");
            httpConn.connect();
            response = httpConn.getResponseCode();
            if (response == HttpURLConnection.HTTP_OK) {
            in = httpConn.getInputStream();
            }
            }
            catch (Exception ex)
            {
            throw new IOException("Error connecting");
            }
            return in;
            }

}

main.xml 编码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ImageView
android:id="@+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
/>
<TextView
android:id="@+id/text"
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>

Mainfest编码:

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

    <uses-sdk android:minSdkVersion="15" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".HttpDownloadActivity"
            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>

在 logcat 中输出

05-21 11:13:53.296: D/AndroidRuntime(620): >>>>>> AndroidRuntime START com.android.internal.os.RuntimeInit <<<<<<
05-21 11:13:53.296: D/AndroidRuntime(620): CheckJNI is ON
05-21 11:13:54.166: D/AndroidRuntime(620): Calling main entry com.android.commands.pm.Pm
05-21 11:13:54.216: D/AndroidRuntime(620): Shutting down VM
05-21 11:13:54.226: D/dalvikvm(620): GC_CONCURRENT freed 101K, 78% free 463K/2048K, paused 1ms+1ms
05-21 11:13:54.236: D/dalvikvm(620): Debugger has detached; object registry had 1 entries
05-21 11:13:54.270: I/AndroidRuntime(620): NOTE: attach of thread 'Binder Thread #3' failed
05-21 11:13:54.836: D/AndroidRuntime(633): >>>>>> AndroidRuntime START com.android.internal.os.RuntimeInit <<<<<<
05-21 11:13:54.836: D/AndroidRuntime(633): CheckJNI is ON
05-21 11:13:55.646: D/AndroidRuntime(633): Calling main entry com.android.commands.am.Am
05-21 11:13:55.686: I/ActivityManager(87): START {act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.HttpDownload/.HttpDownloadActivity} from pid 633
05-21 11:13:55.686: W/WindowManager(87): Failure taking screenshot for (180x300) to layer 21005
05-21 11:13:55.736: W/NetworkManagementSocketTagger(87): setKernelCountSet(10043, 1) failed with errno -2
05-21 11:13:55.746: D/AndroidRuntime(633): Shutting down VM
05-21 11:13:55.766: D/dalvikvm(633): GC_CONCURRENT freed 102K, 77% free 485K/2048K, paused 1ms+2ms
05-21 11:13:55.777: D/dalvikvm(633): Debugger has detached; object registry had 1 entries
05-21 11:13:55.816: I/AndroidRuntime(633): NOTE: attach of thread 'Binder Thread #3' failed
05-21 11:13:56.186: W/System.err(565): java.io.IOException: Error connecting
05-21 11:13:56.186: W/System.err(565):  at com.HttpDownload.HttpDownloadActivity.OpenHttpConnection(HttpDownloadActivity.java:83)
05-21 11:13:56.226: W/System.err(565):  at com.HttpDownload.HttpDownloadActivity.DownloadImage(HttpDownloadActivity.java:51)
05-21 11:13:56.246: I/Process(87): Sending signal. PID: 565 SIG: 3
05-21 11:13:56.246: I/dalvikvm(565): threadid=3: reacting to signal 3
05-21 11:13:56.296: W/System.err(565):  at com.HttpDownload.HttpDownloadActivity.onCreate(HttpDownloadActivity.java:35)
05-21 11:13:56.296: W/System.err(565):  at android.app.Activity.performCreate(Activity.java:4465)
05-21 11:13:56.307: W/System.err(565):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
05-21 11:13:56.307: W/System.err(565):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
05-21 11:13:56.307: W/System.err(565):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
05-21 11:13:56.307: W/System.err(565):  at android.app.ActivityThread.access$600(ActivityThread.java:123)
05-21 11:13:56.316: W/System.err(565):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
05-21 11:13:56.316: I/dalvikvm(87): Jit: resizing JitTable from 4096 to 8192
05-21 11:13:56.326: W/System.err(565):  at android.os.Handler.dispatchMessage(Handler.java:99)
05-21 11:13:56.326: W/System.err(565):  at android.os.Looper.loop(Looper.java:137)
05-21 11:13:56.326: I/dalvikvm(565): Wrote stack traces to '/data/anr/traces.txt'
05-21 11:13:56.346: W/System.err(565):  at android.app.ActivityThread.main(ActivityThread.java:4424)
05-21 11:13:56.346: W/System.err(565):  at java.lang.reflect.Method.invokeNative(Native Method)
05-21 11:13:56.346: W/System.err(565):  at java.lang.reflect.Method.invoke(Method.java:511)
05-21 11:13:56.356: W/System.err(565):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
05-21 11:13:56.366: W/System.err(565):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
05-21 11:13:56.366: W/System.err(565):  at dalvik.system.NativeStart.main(Native Method)
05-21 11:13:56.596: D/dalvikvm(87): GC_CONCURRENT freed 330K, 14% free 11313K/13127K, paused 6ms+10ms
05-21 11:13:56.796: I/Process(87): Sending signal. PID: 565 SIG: 3
05-21 11:13:56.796: I/dalvikvm(565): threadid=3: reacting to signal 3
05-21 11:13:56.806: I/dalvikvm(565): Wrote stack traces to '/data/anr/traces.txt'
05-21 11:13:56.896: I/ActivityManager(87): Displayed com.HttpDownload/.HttpDownloadActivity: +1s169ms
05-21 11:13:57.196: W/InputManagerService(87): Starting input on non-focused client com.android.internal.view.IInputMethodClient$Stub$Proxy@415b29b0 (uid=10013 pid=184)
05-21 11:13:57.246: W/NetworkManagementSocketTagger(87): setKernelCountSet(10013, 0) failed with errno -2

看来我正在赶上,我不知道确切,在这个过程中帮助我提前谢谢你

4

1 回答 1

1

从 url 下载图像可以简单地使用

 URL url = new URL(address);
 Object content = url.getContent();

只需检查Displaying an Image from the web

于 2012-05-21T06:05:36.373 回答