How to set Image as wallpaper with Cropping in android full source code

Hi Folks this demo is regarding how to
    • open the camera ,
    • open the gallery,
    • get the image from asset file and
    • cropping the image,
    • set wallpaper in android. 

I hope it will usefull for newbie.
Please Find the  Link for Download

Open the Camera
Intent For opening the Camera

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

try {
  Uri mImageCaptureUri = null;
  String state = Environment.getExternalStorageState();
  if (Environment.MEDIA_MOUNTED.equals(state)) {
      mImageCaptureUri = Uri.fromFile(tempFile);
  } else {

      mImageCaptureUri = InternalStorageContentProvider
              .CONTENT_URI;
  }
  intent.putExtra(MediaStore.EXTRA_OUTPUT, mImageCaptureUri);
  intent.putExtra("return-data", true);
  startActivityForResult(intent, REQUEST_CODE_TAKE_PICTURE);
} catch (ActivityNotFoundException e) {

  Log.d(TAG, "cannot take picture", e);
}

Uses-Permission in Manifest File
<uses-permission android:name="android.permission.CAMERA" />

Open the Gallery


Open Gallery Intent


Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, REQUEST_CODE_GALLERY);
OnActivityResult After Image Selected

InputStream inputStream = getContentResolver().openInputStream(data.getData());
FileOutputStream fileOutputStream = new FileOutputStream(tempFile);
copyStream(inputStream, fileOutputStream);
fileOutputStream.close();
inputStream.close();

Image From Asset
Code for getting the image from asset folder

AssetManager assetManager = getAssets();
InputStream istr = null;
try {
  istr = assetManager.open(strName);
} catch (IOException e) {
  e.printStackTrace();
}
Bitmap bitmap = BitmapFactory.decodeStream(istr);
Setwallpaper
    • way1 : setWallpaper(bitmap);
    • way2
WallpaperManager mWallpaperManager = WallpaperManager.getInstance(getApplicationContext());
mWallpaperManager.setBitmap(bitmap);


Uses-Permission in Manifest File
<uses-permission android:name="android.permission.SET_WALLPAPER" />


Croppin Image
Intent For Cropping the image
Intent intent = new Intent(this, CropImage.class);
intent.putExtra(CropImage.IMAGE_PATH, tempFile.getPath());
intent.putExtra(CropImage.SCALE, true);


//Here you can set the ratio
intent.putExtra(CropImage.ASPECT_X, 3);
intent.putExtra(CropImage.ASPECT_Y, 2);


startActivityForResult(intent, REQUEST_CODE_CROP_IMAGE);

Where CropImage is custom class. you can find those classes in my attached project

I attached the full source code above.

If you need any help or i mentioned anything wrong, Please give comments your suggestion
Thank you.