Pages

Friday, 13 September 2013

To download the images device local storage from internet



if(ImageUrl.length()>0){
File file=new File(directory,filename);
if(file.exists()&&file.length()==0){
file.delete();
}
if(!file.exists()){
try
{
file.createNewFile();
FileOutputStream ostream = new FileOutputStream(file);
Bitmap bitmap = BitmapFactory.decodeStream(new URL(ImageUrl).openStream());
bitmap.compress(CompressFormat.JPEG, 50, ostream);
ostream.flush();
ostream.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}

Download the multiple files/documents from Internet to android device storage


//here we are checking the directory already exists or not



File directory = new File("file directory name/full path");
        if(!directory.exists()){
            directory.mkdirs();
        }

//for loop to download the multiple files.
    for(int i=0; i<presentationsList.size();i++){
             
           File file=new File(directory, filename);
//here we checking the if file already exists or not. and even if the file size is zero we delete the file and recreate the file.
          if(file.exists() && file.length()==0){
              file.delete();
          }
   
//here is the actual stuff to download the file.

           if(!file.exists()){
             
               try
               {
        URLConnection ucon = new URL(proved the url here).openConnection();
                    InputStream is = ucon.getInputStream();
                     fsize=ucon.getContentLength();
               
                    
                BufferedInputStream bis = new BufferedInputStream(is);
               ByteArrayBuffer baf = new ByteArrayBuffer(ucon.getContentLength());
                    int current = 0;
                    while ((current = bis.read()) != -1) {
                        baf.append((byte) current);
                    }
                   
                    FileOutputStream fos = new FileOutputStream(file);
                    fos.write(baf.toByteArray());
                    fos.flush();
                    fos.close();

               //if the downloaded file size is less than the actual file size then we remove the file

                   if(file.length()<fsize){       
                         if(file.exists())
                             file.delete();
                     }                   
               }
               catch (Exception e)
               {
                   if(file.length()<fsize){       
                        if(file.exists())
                            file.delete();
                    }    
                   e.printStackTrace();
               }
             }
             }

Android custom alert dialog and To fill the device width


final Dialog  dialog=new Dialog(AgendaDetails.this);
         dialog.setTitle("Set reminder");
         dialog.setContentView(R.layout.alertdialoglist);
                        
         ListView listView = (ListView) dialog.findViewById(R.id.listView1);
         listView.setAdapter(new ReminderViewadapter(AgendaDetails.this));
                  

// here we need to set the layout parameter to the alert dialogue.

         WindowManager.LayoutParams lp = new        WindowManager.LayoutParams();
            lp.copyFrom(dialog.getWindow().getAttributes());
            lp.width = WindowManager.LayoutParams.MATCH_PARENT;

            dialog.show();
            dialog.getWindow().setAttributes(lp);

Android device Calendar Integration from Application

To add the event to device calendar using intent:

This intent will open the calnder add/edit the event screen:

                                    Intent intent = new Intent(Intent.ACTION_EDIT);
                                    intent.setType("vnd.android.cursor.item/event");
                                    intent.putExtra("beginTime", stdate);
                                    intent.putExtra("allDay", false);
                                    intent.putExtra("endTime", enddate);
                                    intent.putExtra("title", title_name);
                                    intent.putExtra("description", "");
                                    intent.putExtra("eventLocation", location);

                                    startActivity(intent);



Below provided Event add/remove and Reminder add/remove Uri's work with api level 8 and above .
if your are developing application with below api level 8 you need to change  the content Uri to
 if(Build.VERSION.SDK_INT >= 8){
      Uri.parse("content://com.android.calendar/events")
    }else {
      Uri.parse("content://calendar/events")
    }

To work with calender provider you need to mention the calendar permission in manifest.xml
<uses-permission android:name="android.permission.READ_CALENDAR" />
<uses-permission android:name="android.permission.WRITE_CALENDAR" />

To add the event to device calendar in background:   

This will add the event with help of calendar provider.
If you are developing the application using the api level 14 or above you can directly use the CalendarContract class and it's constants instead of below used strings.

 Uri eventUriString = Uri.parse("content://com.android.calendar/events");
            
             ContentValues eventValues = new ContentValues();

             eventValues.put("calendar_id", 1); // id, We need to choose from our mobile for primary its 1
             eventValues.put("title", title);
             eventValues.put("description", info);
             eventValues.put("eventLocation", place);
             eventValues.put("dtstart", startDate);
             eventValues.put("dtend", endDate);
             eventValues.put("eventTimezone", TimeZone.getDefault().getID());
             eventValues.put("eventStatus", 1); // This information is sufficient for     most entries tentative (0), confirmed (1) or canceled (2):
             eventValues.put("hasAlarm", 1); // 0 for false, 1 for true
        
             Uri eventUri = cr.insert(eventUriString, eventValues);


             
            //to get the last inserted event id
             long eventID = Long.parseLong(eventUri.getLastPathSegment());


To delete the event from calendar id:

If you are developing the application using the api level 14 or above you can directly use the CalendarContract class and it's constants instead of below used strings.
Uri eventUri = Uri.parse("content://com.android.calendar/events");  // or 
         Uri deleteUri = null;
         deleteUri = ContentUris.withAppendedId(eventUri, id);
         int rows = cr.delete(deleteUri, null, null);


To add the reminder for particular Event:

If you are developing the application using the api level 14 or above you can directly use the CalendarContract class and it's constants instead of below used strings. 

ContentValues reminders = new ContentValues();
      reminders.put("event_id", event_id);
      reminders.put("method", 1);//Reminder.METHOD_ALERT constant value is 1
      reminders.put("minutes", mReminders.get(pos));//Passing this as -1 will use the default reminder minutes.
        Uri uri2 = Uri.parse("content://com.android.calendar/reminders");
            getContentResolver().insert(uri2, reminders);   

To get the particular Event reminders:

If you are developing the application using the api level 14 or above you can directly use the CalendarContract class and it's constants instead of below used strings. 

 Uri uri = Uri.parse("content://com.android.calendar/reminders");
    Cursor cursor;
     
     cursor=getContentResolver().query(uri, null,"event_id = ?", new String[]{cal_Id}, null);
       
           if (cursor!=null && cursor.moveToFirst()) {
                do {
                    reminder_BO=new Reminder_BO();
                    reminder_BO.set_id(cursor.getString(cursor.getColumnIndex("_id")));
                    reminder_BO.setEvent_id(cursor.getString(cursor.getColumnIndex("event_id")));
                    reminder_BO.setMinutes(cursor.getString(cursor.getColumnIndex("minutes")));
                    reminder_BO.setMethod(cursor.getString(cursor.getColumnIndex("method")));
         reminders.add(reminder_BO);
                } while (cursor.moveToNext());
            }
          
        cursor.close();

To remove the particular event reminder :


If you are developing the application using the api level 14 or above you can directly use the CalendarContract class and it's constants instead of below used strings. 

    Uri uri = Uri.parse("content://com.android.calendar/reminders");
                                   int rows= getContentResolver().delete(uri, "event_id = ? and _id = ?", new String[]{mEventReminders.get(position).get(pos).getEvent_id(),mEventReminders.get(position).get(pos).get_id()});