Thursday 22 August 2013

Saving Image path URL into SD Card

When you get image path from web url and want to store it on sd card then

Bitmap bitmap;

when you get image path do this code
(get bitmap from image path)

bitmap = DownloadImage(streventphoto); 

private Bitmap DownloadImage(String URL)
    {      
        Bitmap bitmap = null;
        InputStream in = null;      
        try {
            if(!TextUtils.isEmpty(URL)){
                in = OpenHttpConnection(URL);
                bitmap = BitmapFactory.decodeStream(in);
                in.close();   
            }
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        //http://demo.me/demo/upload/bevent/768/20130422133939.jpg
        return bitmap;              
    }


private InputStream OpenHttpConnection(String urlString)
        throws IOException
        {
            InputStream in = null;
            int response = -1;
                   
            URL url = new URL(urlString);
            URLConnection conn = url.openConnection();
                     
            if (!(conn instanceof HttpURLConnection))                   
                throw new IOException("Not an HTTP connection");
            
            try{
                HttpURLConnection httpConn = (HttpURLConnection) conn;
                httpConn.setAllowUserInteraction(false);
                httpConn.setInstanceFollowRedirects(true);
                httpConn.setRequestMethod("GET");
                httpConn.connect();
   
                response = httpConn.getResponseCode();               
                if (response == HttpURLConnection.HTTP_OK) {
                    in = httpConn.getInputStream();                               
                }                   
            }
            catch (Exception ex)
            {
                throw new IOException("Error connecting");          
            }
            return in;   
        }




 now saving image


if(streventphoto.contains("mnt")){
                        // your image path is from sd card
                    }else{
                        boolean success = false;
                        if(bitmap != null){
                            Log.i(tag, "Bitmap.........."+bitmap);
                            try {
                                byte[] bitmapdata;
                                ByteArrayOutputStream stream = new ByteArrayOutputStream();
                                bitmap.compress(Bitmap.CompressFormat.JPEG, 90, stream);
                                bitmapdata = stream.toByteArray();

                                if (bitmapdata != null) {
                                    MyWrite(bitmapdata);
                                    success = true;
                                }

                            } catch (Exception e) {
                                e.printStackTrace();
                                success = false;
                            }
                        }
                        if (success) {
                             Log.i(tag, "Image Saved Successfully");
                            } else {
                                Log.i(tag, "Error during image saving");
                            }  
                    }



this is the function  MyWrite

String currentDateandTime;

    public void MyWrite(byte[] buffer) {
       
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss");
        currentDateandTime = sdf.format(new Date());
        System.gc();
        File sdCard = Environment.getExternalStorageDirectory();
        File directory = new File(sdCard.getAbsolutePath() + "/MyImage");
        directory.mkdirs();
        // Now create the file in the above directory and write the contents
        // into it
        System.out.println("Image path=" + streventphoto);
        File file;
        file = new File(directory, currentDateandTime + ".jpg");

        // File file = new File(directory, "sample.jpg");
        streventphoto = file.getAbsolutePath();
        System.out.println("Path=" + streventphoto);

        try {
            if (!file.exists())
                file.createNewFile();
        } catch (IOException e2) {
            // TODO Auto-generated catch block
            e2.printStackTrace();
        }

        FileOutputStream fOut = null;
        try {
            fOut = new FileOutputStream(file);
        } catch (FileNotFoundException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

        BufferedOutputStream osw = new BufferedOutputStream(fOut);
        try {
            // osw.write(path);
            osw.write(buffer);
            // osw.write(buffer, offset, length);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            osw.flush();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            osw.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }



No comments:

Post a Comment

Comments

Find Hours Diffrence in Kotlin

  In Kotlin, determining the difference in hours between two timestamps is a common task, especially in scenarios involving time-based calcu...