来自画廊的android app壁纸

时间:2012-06-18 11:19:28

标签: android gallery wallpaper

我是Android的新手,我一直在试图弄清楚如何在Android上显示照片。我整个星期都在搜索没有运气,想知道是否有任何Android示例代码或示例如何从图库中为应用程序设置壁纸

谢谢大家

3 个答案:

答案 0 :(得分:0)

使用以下方法从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);
            }

并且在活动结果上写:

    @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

}

答案 1 :(得分:0)

尝试在新的波士顿网站上搜索Android开发教程,我认为41和42教授如何从图库或相机中获取图像,并将其设置为壁纸。试试这个链接http://thenewboston.org/list.php?cat=6。快乐的编码:)

答案 2 :(得分:-1)

请查看代码。

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();
                }

我认为它对你有帮助。