0

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

谢谢你们

4

3 回答 3

0

使用以下方法从图像库中选择图像

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

}
于 2012-06-18T11:25:02.093 回答
0

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

于 2012-06-18T11:31:52.880 回答
-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();
                }

我认为它对你有帮助。

于 2012-06-18T11:23:07.093 回答