Jquery Mobile Support

  • Subscribe to our RSS feed.
  • Twitter
  • StumbleUpon
  • Reddit
  • Facebook
  • Digg

Friday, 13 September 2013

How to Download an image in android

Posted on 08:55 by Unknown
The following code shows how to download a image from a particular URL.

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" /
Email ThisBlogThis!Share to XShare to FacebookShare to Pinterest
Posted in | No comments
Newer Post Older Post Home

0 comments:

Post a Comment

Subscribe to: Post Comments (Atom)

Popular Posts

  • Identifying your mobile visitors from web stats
        As mobile browsers have moved from gimmick to the mainstream over the last few years the job of a web developer has had to evolve to ser...
  • Introduction to JQuery Mobile
    Introduction jQuery Mobile is a user interface framework based on jQuery that works across all popular phones, tablet, e-reader, and desktop...
  • Bye bye analogue telly
       It is with some sadness that I note today sees the turning off of the final UK terrestrial analogue TV transmitter in Northern Ireland. N...
  • What was that about "Don't be evil"?
        I'm sure most readers of this blog will be familiar with the famous Google motto "Don't be evil". Having had the chanc...
  • Slashdot is beyond resuscitation.
        I'm a news junkie. Specifically I'm a tech news junkie. Part of my morning routine involves scanning Google Reader to see what...
  • There will be no iPad killer
        This week brought the news that Nokia have launched their long-rumoured tablet running Windows RT. Despite their woes of the last few ye...
  • All blogs have to start somewhere
    Keyword a word which acts as the key to a cipher or code a word or concept of great significance Geek an unfashionable or socially inept per...
  • MSIE overtaken
        Back in April I wrote a piece about the rise of Google Chrome and the pending loss to MSIE of the number one browser slot. Based on Stat...
  • Is there a relationship between content volume and traffic?
        My exercise in future web traffic prediction last month must have caused some interest among its target audience, because I've been ...
  • Fixed jQuery Mobile footers on Windows Phone 8
        Here's a solution to something which baffled me for a while: making a jQuery Mobile footer that stayed at the bottom of the screen a...

Categories

  • accessibility
  • Android
  • blogging
  • browsers
  • CSS
  • DLA
  • DSP
  • education
  • failblog
  • filtering
  • frequency analysis
  • google
  • homeless
  • HTML
  • infographic
  • internet
  • Javascript
  • jQuery
  • JSON
  • keyword tool
  • maps
  • media
  • mobile
  • Nokia
  • ODO
  • Opera
  • outrage
  • oxford
  • PC
  • personal
  • politics
  • prediction
  • QR codes
  • Raspberry Pi
  • review
  • search engine marketing
  • smartphone
  • snake oil
  • Symbian
  • tablets
  • tech
  • traffic prediction
  • TV
  • words
  • WP8

Blog Archive

  • ▼  2013 (10)
    • ►  November (2)
    • ►  October (1)
    • ▼  September (3)
      • Moodle 2.4
      • Introduction to JQuery Mobile
      • How to Download an image in android
    • ►  July (1)
    • ►  January (3)
  • ►  2012 (18)
    • ►  December (1)
    • ►  November (1)
    • ►  October (2)
    • ►  September (1)
    • ►  August (2)
    • ►  July (1)
    • ►  June (2)
    • ►  May (4)
    • ►  April (2)
    • ►  March (1)
    • ►  February (1)
  • ►  2011 (10)
    • ►  December (1)
    • ►  November (1)
    • ►  October (1)
    • ►  September (1)
    • ►  August (2)
    • ►  February (1)
    • ►  January (3)
  • ►  2010 (15)
    • ►  December (5)
    • ►  November (2)
    • ►  October (4)
    • ►  September (4)
Powered by Blogger.

About Me

Unknown
View my complete profile