0

im new to android and I've been trying to figure out how to display a photo on Android. I've been searching all week with no luck and want to know if there are any android sample code or examples on how to set a wallpaper for the app from the image gallery

thank you all

shark zone
  • 49
  • 5
  • [1] is a somewhat related question which you can refer to. [1]: http://stackoverflow.com/questions/3035050/setting-wallpaper-through-code – sleeping_dragon Jun 18 '12 at 11:22

3 Answers3

0

Use following Method for selecting image from Image Gallary

   //Declare private static final int PICK_IMAGE = 1;

  try {
                Intent intent = new Intent();
                intent.setType("image/*");
                intent.setAction(Intent.ACTION_GET_CONTENT);
                startActivityForResult(Intent.createChooser(intent, "Select Picture"),  PICK_IMAGE);
            } catch (Exception e) {
                Toast.makeText(getApplicationContext(),e.getMessage(),Toast.LENGTH_LONG).show();
                Log.e(e.getClass().getName(), e.getMessage(), e);
            }

And On Activity Result Write :

    @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    switch (requestCode) {
    case PICK_IMAGE:

        if (resultCode == Activity.RESULT_OK) {
            Uri selectedImageUri = data.getData();
            try {
                // OI FILE Manager
                String filemanagerstring = selectedImageUri.getPath();

                // MEDIA GALLERY
                String selectedImagePath = getPath(selectedImageUri);

                if (selectedImagePath != null) {
                    filePath = selectedImagePath;
                } else if (filemanagerstring != null) {
                    filePath = filemanagerstring;
                } else {
                    Toast.makeText(getApplicationContext(), "Unknown path",
                            Toast.LENGTH_LONG).show();

                }

                if (filePath != null) {         

                    decodeFile(filePath);
                } else {
                    bitmap = null;
                }
            } catch (Exception e) {
                Toast.makeText(getApplicationContext(), "Internal  
                     error",Toast.LENGTH_LONG).show();

            }
        }
        break;
    default:
    }



}


     //Decode file() where you will get decoded file and then you can use that image
      //file according to your requirement

   public void decodeFile(String filePath) {

    // Decode image size
    BitmapFactory.Options o = new BitmapFactory.Options();
    o.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(filePath, o);

    // The new size we want to scale to
    final int REQUIRED_SIZE = 1024;

    // Find the correct scale value. It should be the power of 2.
    int width_tmp = o.outWidth, height_tmp = o.outHeight;
    int scale = 1;
    while (true) {
        if (width_tmp < REQUIRED_SIZE && height_tmp < REQUIRED_SIZE)
            break;
        width_tmp /= 2;
        height_tmp /= 2;
        scale *= 2;  
    }

    // Decode with inSampleSize
    BitmapFactory.Options o2 = new BitmapFactory.Options();
    o2.inSampleSize = scale;
    bitmap = BitmapFactory.decodeFile(filePath, o2);

    image.setImageBitmap(bitmap);// set ImageView in your case set Layout  
              //background

}
Harneet Kaur
  • 4,487
  • 1
  • 16
  • 16
0

Try searching the new boston website for android development tutorials,I think tut 41 and 42 teaches how to get an image from gallery or from the camera , and set it as a wallpaper. Try this link http://thenewboston.org/list.php?cat=6. Happy coding:)

user47
  • 395
  • 1
  • 2
  • 16
-1

please Check out the code.

Bitmap mBitmap = BitmapFactory.decodeResource("imageID");

                WallpaperManager myWallpaperManager = WallpaperManager
                        .getInstance(getApplicationContext());

                try {
                    myWallpaperManager.setBitmap(mBitmap); // here your image bitmap.
                    Toast.makeText(SetWallPaper.this, "Wallpaper set",
                            Toast.LENGTH_SHORT).show();
                } catch (IOException e) {
                    Toast.makeText(SetWallPaper.this,
                            "Error setting wallpaper", Toast.LENGTH_SHORT)
                            .show();
                }

I think it help you.

Md Abdul Gafur
  • 6,213
  • 2
  • 27
  • 37