The download class is shown below
//Download Class
package com.sliit.siv;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import org.apache.http.util.ByteArrayBuffer;
import android.os.Environment;
import android.util.Log;
public class DownloadClass {
private static final String TAG = "FileManager";
private final static String PATH = Environment.getExternalStorageDirectory().getPath();
private static String Path= "";
private static String Name= "";
private static String Directory= "";
public static void DownloadResources(String tempPath,String tempName, String tempDirectory)
{
Path=tempPath;
Name=tempName;
Directory=tempDirectory;
startDownloadingThread();
}
public static void startDownloadingThread()
{
Thread thread = new Thread()
{
@Override
public void run()
{
synchronized (this)
{
try
{
DownloadFromUrl(Path,Name,Directory);
}
catch (Exception e)
{
Log.d(TAG, "FK YY: " + e);
}
finally
{
}
}
}
};
thread.start();
}
public static File DownloadFromUrl(String fileURL, String fileName, String courseDirectoryAndType) //this is the downloader method
{
File file = null;
try
{
URL url = new URL(fileURL); //you can write here any link
boolean mExternalStorageAvailable = false;
boolean mExternalStorageWriteable = false;
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state))
{
// We can read and write the file
mExternalStorageAvailable = mExternalStorageWriteable = true;
}
else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state))
{
// We can only read the file
mExternalStorageAvailable = true;
mExternalStorageWriteable = false;
}
else
{
//It may be one of many other states
mExternalStorageAvailable = mExternalStorageWriteable = false;
}
if (mExternalStorageAvailable || mExternalStorageWriteable)
{
File fileDirectory = new File(PATH + "/Moodle/" + courseDirectoryAndType); // create a File object for the parent directory
fileDirectory.mkdirs(); // if directory is not exist, create it.
file = new File(fileDirectory, fileName); // create a File object for the output file
URLConnection ucon = url.openConnection(); // Open a connection to that URL.
InputStream is = ucon.getInputStream(); // Define InputStreams to read from the URLConnection.
BufferedInputStream bis = new BufferedInputStream(is);
ByteArrayBuffer baf = new ByteArrayBuffer(50); //Read bytes to the Buffer until there is nothing more to read(-1).
int current = 0;
while ((current = bis.read()) != -1)
{
baf.append((byte) current);
}
FileOutputStream fos = new FileOutputStream(file); //convert byte to file(.doc,.ppt or .pdf)
fos.write(baf.toByteArray());
fos.close();
}
} catch (IOException e)
{
Log.d(TAG, "Error: " + e);
}
finally
{
}
return file;
}
public boolean checkFileAvailability(String localPath)
{
boolean status=false;
File sdcard = Environment.getExternalStorageDirectory(); //get the SDcard path
File file = new File(sdcard,localPath); //set original file path
if(file.exists())
{
status = true; //if file exist set status 'true'
}
else
{
//status = false;
}
return status;
}
///oookkk------------------------------------------------------------------------------------------------
public static StringBuilder inputStreamToString(InputStream is)
{
//System.out.println("OK 1");
String rLine = "";
StringBuilder answer = new StringBuilder();
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
try {
while ((rLine = rd.readLine()) != null) {
answer.append(rLine);
}
}
catch (IOException e) {
e.printStackTrace();
}
//System.out.println("OK 2");
return answer;
}
//Noifications UI
}
The following is the XML file
//Download.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="downloadPicture"
android:text="Click to start download" >
</Button>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="resetPicture"
android:text="Reset Picture" >
</Button>
</LinearLayout>
<ImageView
android:id="@+id/imageView2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/ic_launcher" >
</ImageView>
</LinearLayout>
The following changes are needed to do to Manifest.xml
//Manifest.xml
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /
0 comments:
Post a Comment