Tuesday 15 October 2013

Set wallpaper from gallery

 final int RESULT_LOAD_IMAGE=100;

On button Click 

Intent i = new Intent(Intent.ACTION_PICK,
                        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        startActivityForResult(i, 100);


@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
         if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
               String wallpaper = selectedWallpaperImage(getApplicationContext(),data);
               WallpaperManager wallpaperManager = WallpaperManager.getInstance(this);
             
               try {
    wallpaperManager.setBitmap(BitmapFactory.decodeFile(wallpaper));
                             //this line of code will set the selected image as your wallpaper...........
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }}
    }


public static String selectedWallpaperImage(Context context,Intent data){
             Uri selectedImage = data.getData();
     String[] filePathColumn = { MediaStore.Images.Media.DATA };

     Cursor cursor = context.getContentResolver().query(selectedImage,
             filePathColumn, null, null, null);
     cursor.moveToFirst();

     int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
     String picturePath = cursor.getString(columnIndex);
     cursor.close();
return picturePath;
}


and add permission  <uses-permission android:name="android.permission.SET_WALLPAPER" />

And if you want direct set image from application resource 

WallpaperManager myWallpaperManager = WallpaperManager
            .getInstance(getApplicationContext());
          try {
           //set wallpaper picture from resource here
           myWallpaperManager.setResource(R.drawable.andr_gp);
           Toast.makeText(getApplicationContext(),"Success set as wallpaper",22).show();
          } catch (IOException e) {
              Toast.makeText(getApplicationContext(),"Error set as wallpaper",22).show();
          } 

Image upload

When i want to upload image into web server 
 GetNameValue nv = new GetNameValue();
String signupop = chngpassop
                        .getOPName(Utility.OPERATION_NAME);


List<NameValuePair> nvp = nv.getMyInfoEditDetail(userfname,
                        userlname, usermobile, userhomephone, userworkphone,
                        userdob, strheight, strwight, streyecolor,
                        strhaircolor, strtimepref, selectedImagePath, user_id,useremail);
                if(!TextUtils.isEmpty(selectedImagePath)){
                    result = httpPost.uploadUserImage(
Utility.SIGNUP_PAGE
                            + signupop, nvp);
                }else{
                    result = httpPost.post(
Utility.SIGNUP_PAGE
                            + signupop, nvp);
                }


my http post class having function for upload image to server as define below

public String uploadUserImage(String op,List<NameValuePair> nvp)
    {
        try
        {
            HttpConnectionParams.setConnectionTimeout(params,TIMEOUT_MILLISEC);
            HttpConnectionParams.setSoTimeout(params,TIMEOUT_MILLISEC);
          
                HttpContext localContext = new BasicHttpContext();
               // System.out.println("Server Link: "+serverurl+op);
                post = new HttpPost(serverurl+op);

                try {
                    MultipartEntity multipartEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);

                    //System.out.println("Upload Image: "+nvp);
                    if(nvp != null)
                    {
                         for(int index=0; index < nvp.size(); index++)
                         {
                             if(nvp.get(index).getName().equalsIgnoreCase("user_photo"))
                            {
                                if(nvp.get(index).getName().equalsIgnoreCase("user_photo"))
                                {
                                  // If the key equals to "U_PHOTO", we use FileBody to transfer the data
                                    multipartEntity.addPart(nvp.get(index).getName(), new FileBody(new File (nvp.get(index).getValue())));
                                }
                                else
                                {
                                    // Normal string data
                                    multipartEntity.addPart(nvp.get(index).getName(), new StringBody(nvp.get(index).getValue()));
                                    }
                                 }
                             else
                             {
                                if(nvp.get(index).getName().equalsIgnoreCase("user_photo"+index))
                                {
                                    //System.out.println("---In if UMI_NAME---");
                                    multipartEntity.addPart(nvp.get(index).getName(), new FileBody(new File (nvp.get(index).getValue())));
                                }
                                else
                                {
                                    // Normal string data
                                    //System.out.println("---In else UMI_NAME---");
                                     multipartEntity.addPart(nvp.get(index).getName(), new StringBody(nvp.get(index).getValue()));
                                }
                             }
                         }
                    }
                    post.setEntity(multipartEntity);
                    response = client.execute(post, localContext);
                    entity = response.getEntity();
                    if(entity != null)
                    {
                        result = EntityUtils.toString(entity);
                    }
                    else
                    {
                        Log.d("Response Failed","Response from server is failed");
                    }
                  
                } catch (IOException e) {
                    e.printStackTrace();
                }

        }catch(Exception ex)
        {
            ex.printStackTrace();
        }
        return result;
      
    }

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...