从 url 服务器问题下载图像,其中包含 android 中的一些其他语言字符?

Downloading image from server issue with url which contains some other language character in android?

我正在从服务器下载图像并与其他 url 一起存储在 sdcard 中,它正在工作,但是对于这个 url 它没有工作我认为问题出在图像的名称上url.

检查下面我用于从服务器下载图像并存储在 sdcard 中的代码;

Url For Image Download and store in sdcard

public class FirstDownloadFileFromURL extends AsyncTask<String, String, String> 
    {

        @Override
        protected void onPreExecute() 
        {

        }

        @Override
        protected String doInBackground(String... f_url) 
        {

            String  dfilename="";

            try 
            {   
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSSS");
                    String datetime = sdf.format(Calendar.getInstance().getTime()); 
                    String spaceurl=f_url[0];
                    String newurl=spaceurl.replaceAll(" ", "%20");
                    BufferedOutputStream out = null;
                    String originalurl=f_url[0];
                    Uri cu = Uri.parse(originalurl);
                    File f2= new File("" + cu);
                    String tempfile=f2.getName();
                    String extension = tempfile.substring(tempfile.indexOf("."));
                    dfilename=datetime+extension;

                    String root = Environment.getExternalStorageDirectory().getAbsolutePath().toString();
                    File SmartAR = new File(root + "/peakmedia", "");
                    if(!SmartAR.exists()){
                        SmartAR.mkdirs();
                    }
                    File outputFile = new File(SmartAR, dfilename);
                    FileOutputStream fos = new FileOutputStream(outputFile);
                    String url1 = newurl;

                    HttpURLConnection conn = (HttpURLConnection) new URL(url1).openConnection();
                    Log.e("Connection url", "--->"+conn.getURL());
                    conn.setDoInput(true);                  
                    conn.setConnectTimeout(200000);
                    conn.setUseCaches(true);
                    conn.connect();
                    int lenghtOfFile = conn.getContentLength();
                    final InputStream in = new BufferedInputStream(conn.getInputStream(), 5*1024); // buffer size
                    out = new BufferedOutputStream(fos, 5*1024);
                    int b;
                    long total = 0;
                    byte data[] = new byte[5*1024];
                    while ((b = in.read(data)) != -1) 
                    {
                        total += b;
                        publishProgress(""+(int)((total*100)/lenghtOfFile));
                        out.write(data,0,b);
                    }
                    out.close();
                    in.close();
                    conn.disconnect();

            } catch (final MalformedURLException e) {
                showError("Error : MalformedURLException " + e);        
                e.printStackTrace();
            } catch (final IOException e) {
                showError("Error : IOException " + e);          
                e.printStackTrace();
            }
            catch (final Exception e) {
                showError("Error : Please Check Your Wifi connection " + e);
            }       
            return dfilename;
        }

        @Override
        protected void onProgressUpdate(String... progress) 
        {
             progressbar.setProgress(Integer.parseInt(progress[0]));
             textprogressbar.setText(String.valueOf(Integer.parseInt(progress[0])+"%"));
        }

您应该使用 URLEncoder.encode(),它会将 space 更改为 %20(删除您的 replaceAll)和其他字符。

您的 URL 将变为:https://secure.peakmedia.at/studio/img/thumbnails/original/%C3%96ffnungszeiten%20Test-638.jpg