Android-da URL-dan rasmni yuklash

Men Volley JSONObjectRequest'dan olingan URLdan rasm yuklashga harakat qilaman.

Bu mening JSON so'rovim:

    //creates the post request and posts
    JsonObjectRequest getRandomImageURL = new JsonObjectRequest(Request.Method.POST, url_image,null,
            new Response.Listener() {
                @Override
                public void onResponse(JSONObject response) {

                   Log.d("ServerResponse","ATM ImageURL"+response.toString());
                    try {
                        String image = response.getString("Image");
                        Log.d("Image","ImageURL "+imgLink+image);



                        imgAd = (ImageView) findViewById(R.id.imgAd);
                        String imgURL = imgLink+image;
                        new DownloadImageTask(imgAd).execute(imgURL);




                    } catch (JSONException e) {
                        e.printStackTrace();
                    }


                }

            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                   //error
                    Log.d("Error.Response","Error"+ error.getLocalizedMessage());


                }
            }
    ) {
        @Override
        public Map getHeaders() throws AuthFailureError {
            HashMap headers = new HashMap<>();
            headers.put("Content-type", "application/json");
            headers.put("Accept", "application/json");
            headers.put("User-Agent", "Mozilla/5.0");
            headers.put("Content-length", dataStream.toString());
            return headers;
        }
        @Override
        public byte[] getBody() {
            try {
                return mRequestBody == null ? null : mRequestBody.getBytes("utf-8");
            } catch (UnsupportedEncodingException uee) {
                VolleyLog.wtf("Unsupported Encoding while trying to get the bytes of %s using %s", mRequestBody, "utf-8");
                return null;
            }
        }
    };
    Volley.newRequestQueue(this).add(getRandomImageURL);
}

Va URLdan rasmni yuklash uchun foydalanadigan bu narsa:

private class DownloadImageTask extends AsyncTask {
    ImageView bmImage;

    public DownloadImageTask(ImageView bmImage) {
        this.bmImage = bmImage;
    }

    protected Bitmap doInBackground(String... urls) {
        String urldisplay = urls[0];
        Bitmap mIcon11 = null;
        try {
            InputStream in = new java.net.URL(urldisplay).openStream();
            mIcon11 = BitmapFactory.decodeStream(in);
        } catch (Exception e) {
            Log.e("Error", e.getMessage());
            e.printStackTrace();
        }
        return mIcon11;
    }

    protected void onPostExecute(Bitmap result) {
        bmImage.setImageBitmap(result);
    }
}

Men quyidagi istisnolardan foydalanishni davom ettiraman:

W/System.err: java.io.FileNotFoundException:    https://www.lamdacardservices.com/offers/CINEMA Logo.jpg
W/System.err:     at com.android.okhttp.internal.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:197)
W/System.err:     at com.android.okhttp.internal.http.DelegatingHttpsURLConnection.getInputStream(DelegatingHttpsURLConnection.java:210)
W/System.err:     at com.android.okhttp.internal.http.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:25)
W/System.err:     at java.net.URL.openStream(URL.java:470)
W/System.err:     at com.example.epavlos.lamda.map_activities.AtmMapActivity$DownloadImageTask$override.doInBackground(AtmMapActivity.java:327)
W/System.err:     at com.example.epavlos.lamda.map_activities.AtmMapActivity$DownloadImageTask$override.access$dispatch(AtmMapActivity.java)
W/System.err:     at com.example.epavlos.lamda.map_activities.AtmMapActivity$DownloadImageTask.doInBackground(AtmMapActivity.java:0)
W/System.err:     at com.example.epavlos.lamda.map_activities.AtmMapActivity$DownloadImageTask.doInBackground(AtmMapActivity.java:317)
W/System.err:     at android.os.AsyncTask$2.call(AsyncTask.java:288)
W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
W/System.err:     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
W/System.err:     at java.lang.Thread.run(Thread.java:818)

Men noto'g'ri qilayotgan narsamga yordam bera oladimi? Yoki bunga erishishning engil yo'li bo'lsa ham, ehtimol, voleybol yoki biror narsadan foydalanish mumkinmi?

0

8 javoblar

Buni ko'ring:

 String temp = "https://www.lamdacardservices.com/offers/CINEMA Logo.jpg";
    temp = temp.replaceAll(" ", "%20");
    URL sourceUrl = new URL(temp);
1
qo'shib qo'ydi

Buni ko'ring:

 String temp = "https://www.lamdacardservices.com/offers/CINEMA Logo.jpg";
    temp = temp.replaceAll(" ", "%20");
    URL sourceUrl = new URL(temp);
1
qo'shib qo'ydi

Bundan tashqari, ImageLoaderni kuchli tasvirni yuklash va keshlash vositasi sifatida ishlatishingiz mumkin.

Gradle qaramligi:

kompilyatsiya 'com.nostra13.universalimageloader: universal-image-loader: 1.9.5'

Foydalanish uchun:

ImageLoader imageLoader = ImageLoader.getInstance ();//Singleton nusxasini oling

imageLoader.displayImage (imageUri, imageView);

Source: https://github.com/nostra13/Android-Universal-Image-Loader

1
qo'shib qo'ydi
U IMAGE dudeini yuklab olishni xohlaydi.
qo'shib qo'ydi muallif Babul Patel, manba
Yup, u download qilmoqda, lekin, aslida, oxirida ImageView-ga nozil qilingan tasvirni ko'rsatmoqda. bmImage.setImageBitmap (natija);
qo'shib qo'ydi muallif owlein, manba

Bundan tashqari, ImageLoaderni kuchli tasvirni yuklash va keshlash vositasi sifatida ishlatishingiz mumkin.

Gradle qaramligi:

kompilyatsiya 'com.nostra13.universalimageloader: universal-image-loader: 1.9.5'

Foydalanish uchun:

ImageLoader imageLoader = ImageLoader.getInstance ();//Singleton nusxasini oling

imageLoader.displayImage (imageUri, imageView);

Source: https://github.com/nostra13/Android-Universal-Image-Loader

1
qo'shib qo'ydi
U IMAGE dudeini yuklab olishni xohlaydi.
qo'shib qo'ydi muallif Babul Patel, manba
Yup, u download qilmoqda, lekin, aslida, oxirida ImageView-ga nozil qilingan tasvirni ko'rsatmoqda. bmImage.setImageBitmap (natija);
qo'shib qo'ydi muallif owlein, manba

Siz kabi muammolarni hal qiladigan URLni kodlashingiz kerak. Buning bir usuli

Uri.encode("https://www.lamdacardservices.com/offers/CINEMA Logo.jpg")
0
qo'shib qo'ydi

https://www.lamdacardservices.com/offers/CINEMA Logo.jpg

URL manzilingizni tekshiring, u rasm nomining orasidagi bo'sh joyni URL kodlashni ishlatishga urinadi

https://www.lamdacardservices.com/offers/CINEMA%20Logo.jpg

ushbu URL bilan urinib ko'ring va menga xabar bering.

Faqat bo'sh joyingizni % 20 bilan almashtiring

0
qo'shib qo'ydi
uslublar FileNotFound Exception-ni beradi
qo'shib qo'ydi muallif pavlos, manba

https://www.lamdacardservices.com/offers/CINEMA Logo.jpg

URL manzilingizni tekshiring, u rasm nomining orasidagi bo'sh joyni URL kodlashni ishlatishga urinadi

https://www.lamdacardservices.com/offers/CINEMA%20Logo.jpg

ushbu URL bilan urinib ko'ring va menga xabar bering.

Faqat bo'sh joyingizni % 20 bilan almashtiring

0
qo'shib qo'ydi
uslublar FileNotFound Exception-ni beradi
qo'shib qo'ydi muallif pavlos, manba

Buning uchun Glide'dan foydalaning, eng yaxshi uchinchi tomon kutubxonalaridan biri

https://github.com/bumptech/glide

0
qo'shib qo'ydi