Reputation: 1589
I am downloading image from server and store in sdcard with other url it was working but with this url it was not working i think the issue is with the name of image which is in url.
Check below code which i have used for download image from server and store in 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])+"%"));
}
Upvotes: 0
Views: 68
Reputation: 10304
You should use URLEncoder.encode()
, it'll change space to %20 (remove your replaceAll
) and other characters.
Your URL will become : https://secure.peakmedia.at/studio/img/thumbnails/original/%C3%96ffnungszeiten%20Test-638.jpg
Upvotes: 1