We all want to set some photo that we have captured with our camera, some moment, on our home screen. This can be done through our app as well. This is an example where the user can set some image as background using phones image gallery. In very few steps, this procedure will be explained in detail how to implement in your project.
NOTE: We are going to use ButterKnife Library also, for binding the Views from the XML, like buttons, text views, edit text etc in our java classes.
ButterKnife by Jake Wharton Library with examples
At first, we must create a new project in Android Studio. After that in our example, everything is happening in our MainActivity. It is a simple example of selecting image from gallery, setting on our screen in the application for preview, and setting the same image as phones background.
MainActivity:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
public class MainActivity extends AppCompatActivity { @BindView(R.id.image) ImageView image; private static final int RESULT_LOAD_IMG = 1234; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ButterKnife.bind(this); } @OnClick(R.id.getImage) public void onGetImageClick() { Intent photoPickerIntent = new Intent(Intent.ACTION_PICK); photoPickerIntent.setType("image/*"); startActivityForResult(photoPickerIntent, RESULT_LOAD_IMG); } @OnClick(R.id.setAsWallpaper) public void onSetAsWallpaperClick() { image.buildDrawingCache(); Bitmap bitmapImage = image.getDrawingCache(); WallpaperManager wallpaperManager = WallpaperManager.getInstance(this); try { wallpaperManager.setBitmap(bitmapImage); Toast.makeText(this, "Image set as wallpaper", Toast.LENGTH_SHORT).show(); } catch (IOException e) { e.printStackTrace(); } } @Override protected void onActivityResult(int reqCode, int resultCode, Intent data) { super.onActivityResult(reqCode, resultCode, data); if (resultCode == RESULT_OK) { try { final Uri imageUri = data.getData(); final InputStream imageStream = getContentResolver().openInputStream(imageUri); final Bitmap selectedImage = BitmapFactory.decodeStream(imageStream); image.setImageBitmap(rotateImageIfRequired(selectedImage, imageUri)); } catch (FileNotFoundException e) { e.printStackTrace(); Toast.makeText(this, "Something went wrong", Toast.LENGTH_LONG).show(); } catch (IOException e) { e.printStackTrace(); } } else { Toast.makeText(this, "You haven't picked Image", Toast.LENGTH_LONG).show(); } } private Bitmap rotateImageIfRequired(Bitmap img, Uri selectedImage) throws IOException { InputStream input = getContentResolver().openInputStream(selectedImage); ExifInterface ei; if (Build.VERSION.SDK_INT > 23) ei = new ExifInterface(input); else ei = new ExifInterface(selectedImage.getPath()); int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL); switch (orientation) { case ExifInterface.ORIENTATION_ROTATE_90: return rotateImage(img, 90); case ExifInterface.ORIENTATION_ROTATE_180: return rotateImage(img, 180); case ExifInterface.ORIENTATION_ROTATE_270: return rotateImage(img, 270); default: return img; } } private Bitmap rotateImage(Bitmap img, int degree) { Matrix matrix = new Matrix(); matrix.postRotate(degree); Bitmap rotatedImg = Bitmap.createBitmap(img, 0, 0, img.getWidth(), img.getHeight(), matrix, true); img.recycle(); return rotatedImg; } }<span data-mce-type="bookmark" id="mce_SELREST_start" data-mce-style="overflow:hidden;line-height:0" style="overflow:hidden;line-height:0" ></span> |
First section is the Binding the views with the XML resources. This is done throughout the ButterKnife library that i strongly recommend for using. Than is the static int variable, RESULT_LOAD_IMAGE which is used for onActivityResult() for getting the image form the Gallery.
In onCreate() we are adding only the ButterKnife library for binding the XML views in our class.
With @onClick annotation (another ButterKnife feature) we set the intent where we want to select image from the gallery. The intent starts the phones gallery for us to choose from. The previously declared and initialed static int number is used as a code for retrieving the image later in our activity., when we select the wanted image.
In onActivityResault(), we get the selected image and we set on the ImageView for preview. Also, here we use rotateImageIfRequired() method for image rotating if necessary.
In the end, on setAsWallpaper() button click, the image will be set as phones background or wallpaper.
And also, the MainActivity layout file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <Button android:id="@+id/getImage" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginBottom="10dp" android:layout_marginTop="30dp" android:text="@string/getImage" /> <ImageView android:id="@+id/image" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_above="@id/setAsWallpaper" android:layout_below="@id/getImage" android:layout_centerInParent="true" android:layout_marginEnd="16dp" android:layout_marginStart="16dp" /> <Button android:id="@+id/setAsWallpaper" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:layout_marginBottom="30dp" android:layout_marginTop="10dp" android:text="@string/set_as_wallpaper" /> </RelativeLayout> |