2

URLから写真を取得しようとしましたが、写真を取得できませんでした。

現在のコード:

public class IndexActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    ImageView i = (ImageView) findViewById(R.id.imageView1);
    Bitmap bitmap = DownloadImage("http://www.gophoto.it/view.php?i=http://1.bp.blogspot.com/-2LTvCCufBKc/T3L3KgcTj2I/AAAAAAAABbQ/Ki60e1LU9sE/s1600/Sachin%2BTendulkar.png");
    i.setImageBitmap(bitmap);
}

private InputStream OpenHttpConnection(String urlString) throws IOException {
    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;
}

private Bitmap DownloadImage(String URL) {
    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;
    }
}

そしてこれは locCat です:

08-01 08:53:17.818: D/ActivityThread(12315): handleResumeActivity now pri:0
08-01 08:53:17.818: D/ActivityThread(12315): handleResumeActivity set pri:0
08-01 08:53:20.268: W/System.err(12315): java.io.IOException: Error connecting
08-01 08:53:20.268: W/System.err(12315):    at tr.com.turkcell.shmobile.IndexActivity.OpenHttpConnection(IndexActivity.java:70)
08-01 08:53:20.268: W/System.err(12315):    at tr.com.turkcell.shmobile.IndexActivity.DownloadImage(IndexActivity.java:79)
08-01 08:53:20.268: W/System.err(12315):    at tr.com.turkcell.shmobile.IndexActivity.onCreate(IndexActivity.java:32)
08-01 08:53:20.268: W/System.err(12315):    at android.app.Activity.performCreate(Activity.java:5008)
08-01 08:53:20.268: W/System.err(12315):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
08-01 08:53:20.268: W/System.err(12315):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
08-01 08:53:20.268: W/System.err(12315):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
08-01 08:53:20.268: W/System.err(12315):    at android.app.ActivityThread.access$600(ActivityThread.java:130)
08-01 08:53:20.268: W/System.err(12315):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
08-01 08:53:20.278: W/System.err(12315):    at android.os.Handler.dispatchMessage(Handler.java:99)
08-01 08:53:20.278: W/System.err(12315):    at android.os.Looper.loop(Looper.java:137)
08-01 08:53:20.278: W/System.err(12315):    at android.app.ActivityThread.main(ActivityThread.java:4754)
08-01 08:53:20.278: W/System.err(12315):    at java.lang.reflect.Method.invokeNative(Native Method)
08-01 08:53:20.278: W/System.err(12315):    at java.lang.reflect.Method.invoke(Method.java:511)
08-01 08:53:20.278: W/System.err(12315):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
08-01 08:53:20.278: W/System.err(12315):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
08-01 08:53:20.278: W/System.err(12315):    at dalvik.system.NativeStart.main(Native Method)
08-01 08:53:20.278: D/ActivityThread(12315): handleResumeActivity now pri:0
08-01 08:53:20.278: D/ActivityThread(12315): handleResumeActivity set pri:0
4

6 に答える 6

1

この単純なものを試してください:

public static Drawable LoadImageFromWebOperations(String url) {
    try {
        InputStream is = (InputStream) new URL(url).getContent();
        Drawable d = Drawable.createFromStream(is, "src name");
        return d;
    } catch (Exception e) {
        return null;
    }
}

オンラインになっているので、インターネットのアクセス許可を確認してください。

権限を追加:

<uses-permission android:name="android.permission.INTERNET" />
于 2013-08-01T06:26:31.863 に答える
0
loadImage("http://relinjose.com/directory/filename.png");

どうぞ

void loadImage(String image_location) {
    URL imageURL = null;
    if (image_location != null) {
        try {
            imageURL = new URL(image_location);         
            HttpURLConnection connection = (HttpURLConnection) imageURL
                    .openConnection();
            connection.setDoInput(true);
            connection.connect();
            InputStream inputStream = connection.getInputStream();
            bitmap = BitmapFactory.decodeStream(inputStream);// Convert to bitmap
            ivdpfirst.setImageBitmap(bitmap);
        } catch (IOException e) {
            e.printStackTrace();
        }
    } else {
        //set any default
    }
}
于 2013-08-01T06:23:13.477 に答える
0

よく書かれたUrlImageViewHelperライブラリを試すことができます:

https://github.com/koush/UrlImageViewHelper

使い方は簡単です:

UrlImageViewHelper.setUrlDrawable(imageView, "http://example.com/image.png");
于 2013-08-01T06:25:22.577 に答える
0

URL からの Android 画像読み込みの最適なチュートリアル:

試してみてください:このコードは役に立ちます

于 2013-08-01T06:06:16.527 に答える
0

画像の読み込みに関しては以下の可能性があります

1-画像は基本認証で保護されています

2- URL が無効です

最初に URL を変更して、アクションが完璧であることを確認し、画像が表示されるようにします。

2 番目のシナリオでは、AQUERY (Android クエリ)を使用することをお勧めします 。最新の jar をダウンロードしてプロジェクトに追加し、基本認証をバイパスするような方法で使用します。

private AQuery mAquery;
mAquery=new AQuery(context);
BasicHandle handle = new BasicHandle("Username", "password");
mAquery.id(YourImageView).auth(handle).image(ImagePath,true,true,400,0,null,0,0.0f);
于 2013-08-01T06:18:21.737 に答える