Monday, December 19, 2011

Get ImageFile form server and Store onto Device's memory and display it

public class DataStoreActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.main);
        DownloadFile("image file path www.");
        byte b[] = readInternalStoragePrivate("download.jpg");
       
        Bitmap bmp = BitmapFactory.decodeByteArray(b, 0, b.length);
       
        ImageView imageView = new ImageView(this);
        imageView.setImageBitmap(bmp);
        setContentView(imageView);
    }

    /**
     * Reads a file from internal storage
     * @param filename the file to read from
     * @return the file content
     */
    public byte[] readInternalStoragePrivate(String filename) {
        int len = 1024;
        byte[] buffer = new byte[len];
        try {
            FileInputStream fis = openFileInput(filename);
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            int nrb = fis.read(buffer, 0, len); // read up to len bytes
            while (nrb != -1) {
                baos.write(buffer, 0, nrb);
                nrb = fis.read(buffer, 0, len);
            }
            buffer = baos.toByteArray();
            fis.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return buffer;
    }
    /**
     * Writes content to internal storage making the content private to
     * the application. The method can be easily changed to take the MODE
     * as argument and let the caller dictate the visibility:
     * MODE_PRIVATE, MODE_WORLD_WRITEABLE, MODE_WORLD_READABLE, etc.
     *
     * @param filename - the name of the file to create
     * @param content - the content to write
     */
    public void writeInternalStoragePrivate(
            String filename, byte[] content) {
        try {
            //MODE_PRIVATE creates/replaces a file and makes
            //  it private to your application. Other modes:
            //    MODE_WORLD_WRITEABLE
            //    MODE_WORLD_READABLE
            //    MODE_APPEND
            FileOutputStream fos =
               openFileOutput(filename, Context.MODE_PRIVATE);
            fos.write(content);
            fos.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    /**
     * Delete internal private file
     * @param filename - the filename to delete
     */
    public void deleteInternalStoragePrivate(String filename) {
        File file = getFileStreamPath(filename);
        if (file != null) {
            file.delete();
        }
    }

    public  void DownloadFile(String fileURL) {
        try {
              // File file = new File(fileName,);
              // file.createNewFile();
             //  FileOutputStream f = new FileOutputStream(file);
            FileOutputStream f = openFileOutput("download.jpg", Context.MODE_PRIVATE);
            URL u = new URL(fileURL);
            HttpURLConnection c = (HttpURLConnection) u.openConnection();
            c.setRequestMethod("GET");
            c.setDoOutput(true);
            c.connect();
        

            InputStream in = c.getInputStream();

            byte[] buffer = new byte[1024];
            int len1 = 0;
            while ((len1 = in.read(buffer)) > 0) {
                f.write(buffer, 0, len1);
            }
            f.close();
          // Log.d("Download Completed", )
        } catch (Exception e) {
           e.printStackTrace();
        }

    }
}

No comments:

Post a Comment

content -->