2013-04-09 59 views
3

我正在开发一个应用程序,显示不同的照片从服务器和用户可以设置选定的照片作为其设备的壁纸我用给定的代码来设置壁纸它的工作,但图像设置不正确,它不适合屏幕。我用这个代码。如何在android中使用编码设置壁纸?

  String dirPath = getFilesDir().toString(); 

     String folder = mPhotos.get(nextPosition - 1).getCategory(); 
     String filePath = dirPath + "/PhotoViewer/" + folder + "/" 
       + mPhotos.get(nextPosition - 1).getFileName(); 
     File imageFile = new File(filePath); 
     Bitmap bitmap = BitmapFactory.decodeFile(imageFile 
       .getAbsolutePath()); 
     WallpaperManager myWallpaperManager = WallpaperManager 
       .getInstance(getApplicationContext()); 
     try { 
      myWallpaperManager.setBitmap(bitmap); 
      Toast.makeText(PhotoActivity.this, "Wallpaper set", 
        Toast.LENGTH_SHORT).show(); 
     } catch (IOException e) { 
      Toast.makeText(PhotoActivity.this, "Error setting wallpaper", 
        Toast.LENGTH_SHORT).show(); 
     } 

请帮我一把。

回答

3

您可以尝试调整您的位图这样

DisplayMetrics displayMetrics = new DisplayMetrics(); 
    getWindowManager().getDefaultDisplay().getMetrics(displayMetrics); 
    int height = displayMetrics.heightPixels; 
    int width = displayMetrics.widthPixels << 1; // best wallpaper width is twice screen width 

    // First decode with inJustDecodeBounds=true to check dimensions 
    final BitmapFactory.Options options = new BitmapFactory.Options(); 
    options.inJustDecodeBounds = true; 
    BitmapFactory.decodeFile(path, options); 

    // Calculate inSampleSize 
    options.inSampleSize = calculateInSampleSize(options, width, height); 

    // Decode bitmap with inSampleSize set 
    options.inJustDecodeBounds = false; 
    Bitmap decodedSampleBitmap = BitmapFactory.decodeFile(path, options); 

    WallpaperManager wm = WallpaperManager.getInstance(this); 
    try { 
     wm.setBitmap(decodedSampleBitmap); 
    } catch (IOException e) { 
     Log.e(TAG, "Cannot set image as wallpaper", e); 
    } 

你calculateInSampleSize类可以是这样的

public static int calculateInSampleSize(
      BitmapFactory.Options options, int reqWidth, int reqHeight) { 
    // Raw height and width of image 
    final int height = options.outHeight; 
    final int width = options.outWidth; 
    int inSampleSize = 1; 

    if (height > reqHeight || width > reqWidth) { 

     // Calculate ratios of height and width to requested height and width 
     final int heightRatio = Math.round((float) height/(float) reqHeight); 
     final int widthRatio = Math.round((float) width/(float) reqWidth); 

     // Choose the smallest ratio as inSampleSize value, this will guarantee 
     // a final image with both dimensions larger than or equal to the 
     // requested height and width. 
     inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio; 
    } 

    return inSampleSize; 
} 

参考this链接,进一步澄清

+0

calculateInSampleSize(选项,宽度,高度);这是什么 ?这个函数显示你在代码中没有提到这个函数的错误。 – nishitpatel 2013-04-09 04:23:27

23

设定下的Android使用壁纸代码:通过使用WallpaperManager类

Button buttonSetWallpaper = (Button)findViewById(R.id.set); 
ImageView imagePreview = (ImageView)findViewById(R.id.preview); 
imagePreview.setImageResource(R.drawable.five); 

buttonSetWallpaper.setOnClickListener(new Button.OnClickListener() { 
    @Override 
    public void onClick(View arg0) { 
     // TODO Auto-generated method stub 
     WallpaperManager myWallpaperManager 
     = WallpaperManager.getInstance(getApplicationContext()); 
     try { 
      myWallpaperManager.setResource(R.drawable.five); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 
}); 

需要设置允许在清单:

<uses-permission android:name="android.permission.SET_WALLPAPER"/>