i am downloading the file from server and store in sdcard but some times for net connection files are not downloaded completely so i want to check video file is corrupted or not if it is corrupted than delete that file and re download from server how can i check video file is corrupted in sdcard
Code for check image is corrupt
Bitmap bmp =loadResizedBitmap(path, imageWidth, imageHeight, false);
if(bmp!=null)
{
imageview.setImageDrawable(null);
imageview.setImageBitmap(bmp);
}
else
{
// file is corrupt
}
MyDownload.java
public class FirstDownloadFileFromURL extends AsyncTask<String, String, String>
{
@Override
protected void onPreExecute()
{
progressbar.setMax(100);
}
@Override
protected String doInBackground(String... f_url)
{
try
{
String spaceurl=f_url[0];
String newurl=spaceurl.replaceAll(" ", "%20");
BufferedOutputStream out = null;
Uri u = Uri.parse(newurl);
File f1 = new File("" + u);
String originalurl=f_url[0];
Uri cu = Uri.parse(originalurl);
File f2= new File("" + cu);
filename=f2.getName();
Log.i("DownloadFile", "DownloadURL:" +newurl.replaceAll(" ", "%20"));
Log.i("DownloadFile", "filename: " + filename);
File SmartAR;
if(ssdcard.equals("true"))
{
root = Environment.getExternalStorageDirectory().getAbsolutePath();
new File(root + "/montorwerbung").mkdirs();
SmartAR = new File(root + "/montorwerbung", "");
}
else
{
File direct = new File(Environment.getDataDirectory()+"/montorwerbung");
if(!direct.exists())
{
if(direct.mkdir()); //directory is created;
}
SmartAR = new File(direct + "/montorwerbung", "");
}
Log.i("smart ar", "filename: " +SmartAR);
// have the object build the directory structure, if needed.
SmartAR.mkdirs();
// create a File object for the output file
File outputFile = new File(SmartAR, filename);
// now attach the OutputStream to the file object, instead
// of a String representation
FileOutputStream fos = new FileOutputStream(outputFile);
String url1 = newurl;
HttpURLConnection conn = (HttpURLConnection) new URL(url1).openConnection();
conn.setDoInput(true);
conn.connect();
int lenghtOfFile = conn.getContentLength();
final InputStream in = new BufferedInputStream(conn.getInputStream(), 1024); // buffer size
// 1KB
out = new BufferedOutputStream(fos, 1024);
int b;
long total = 0;
byte data[] = new byte[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 null;
}
@Override
protected void onProgressUpdate(String... progress)
{
progressbar.setProgress(Integer.parseInt(progress[0]));
textprogressbar.setText(String.valueOf(Integer.parseInt(progress[0])+"%"));
}
@SuppressWarnings("deprecation")
@Override
protected void onPostExecute(String file_url)
{
}
}
Here is what I did