Domanda

I have trouble with download image from server in my android app. If i try to download image from https://www.morroccomethod.com/components/com_virtuemart/shop_image/category/resized/Trial_Sizes_4e4ac3b0d3491_175x175.jpg

My Code -

bitmap = BitmapFactory.decodeStream((InputStream) new URL(url)
                .getContent());

It return null means image not downloading on 4.0.3 but image downloading successfully on 2.2 I think there may be problem with os version.

Now i want anyone to help and guide me for the same.

È stato utile?

Soluzione

Write below code into your activity.java file's onCreate method after setcontentview().

if (android.os.Build.VERSION.SDK_INT > 9) {
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);
}

Altri suggerimenti

Between 2.2 and 4.0.0 there were some changes regarding what you could do on the UI thread.

From your code snipit I can not tell what thread you are doing this on, but I would expect that this is the same problem.

Try loading your image using an AsyncTask, as you can not perform this http action on the UI thread.

Please confirm about Network on UI Thread Exception and make sure that you are using AsyncTask. Try the same code with AsyncTask, this will help you.

Try this code.

try
        {
            imageView.setImageDrawable(grabImageFromUrl(imageUrl));
        }
        catch (Exception e)
        {
            Log.i("CATCH", "ImageDrawable");
            e.printStackTrace();
        }

and method code is::

private Drawable grabImageFromUrl(String imageUrlInput) throws MalformedURLException, IOException, Exception
    {
        return Drawable.createFromStream((InputStream)new URL(imageUrlInput).getContent(), "src");
    }

I have created this code for you, try it worked at my end...

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.ImageView;

public class image extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Bitmap bitmap = DownloadImage("https://www.morroccomethod.com/components/com_virtuemart/shop_image/category/resized/Trial_Sizes_4e4ac3b0d3491_175x175.jpg");
        ImageView img = (ImageView) findViewById(R.id.img);
        img.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;
    }
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top