2012-12-03 50 views
0

我有一個應用程序,我在SD卡的文件夾中有一些下載的圖像。我想將它保存爲壁紙。打開保存在SD卡中的圖像,可以選擇將其裁切爲圖庫以應用爲牆紙

使用下面的代碼用戶可以將其設置爲壁紙。

WallpaperManager myWallpaperManager = WallpaperManager.getInstance(context); 
myWallpaperManager.setBitmap(loadedImage); 

然而,這並沒有帶來的任何UI用戶從圖庫應用程序中選擇一個圖像時設置壁紙選擇像裁剪操作的圖像的一部分。我希望我的代碼能夠觸發這樣的操作。當用戶點擊我應用程序中的按鈕時,我想將它們帶到帶有裁剪選項的圖庫應用程序來設置壁紙。

請讓我知道如何做到這一點。謝謝。

+0

哪個Android版本是你的目標? – Shinigamae

+0

Hi @Shinigamae:我想從2.1開始支持。但是如果只能從某個特定的版本開始,那麼我可以。 – Vinodtiru

+0

我有一個簡單的項目,在2.3上。我允許用戶從他的圖書館中選擇一張圖片(之前從網站上保存),然後允許他裁剪該圖片。但是我發現它在Android 3.0和4.0上效果不佳。那麼需要一些解決方法。 – Shinigamae

回答

1

你可以試試這個:

  1. 要從庫中選擇(包括SD卡) - 無效selectPhoto()

    Intent intent = new Intent(); 
    intent.setType("image/*"); 
    intent.setAction(Intent.ACTION_GET_CONTENT); 
    startActivityForResult(Intent.createChooser(intent, "Choose photo to upload"), PICK_FROM_FILE); 
    
  2. 要啓動作物的行動 - void doCrop()

    Intent intent = new Intent("com.android.camera.action.CROP"); 
    intent.setType("image/*"); 
    
    // Check if there is image cropper application installed. 
    List<ResolveInfo> list = getPackageManager().queryIntentActivities(intent, 0); 
    
    int size = list.size(); 
    
    // If no cropper application is found, throw a message. 
    if (size == 0) {    
        Toast.makeText(this, "Can not find image crop app", Toast.LENGTH_SHORT).show(); 
        return; 
    
    // If there're cropper applications found, use the first 
    } else { 
    
        // Specify image path and cropping parameters 
        intent.setData(mImageCaptureUri); 
        intent.putExtra("outputX", 0); 
        intent.putExtra("outputY", 0); 
        intent.putExtra("return-data", true); 
    
        Intent i = new Intent(intent); 
        ResolveInfo res = list.get(0); 
        i.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name)); 
        startActivityForResult(i, CROPPED_IMAGE); 
    
  3. 拉手活動結果 - 無效onActivityResult(INT requestCode,INT resultCode爲,意圖數據)

    if (resultCode != RESULT_OK) return; 
    
    switch (requestCode) { 
        case PICK_FROM_FILE: 
         mImageCaptureUri = data.getData(); 
         doCrop(); 
         break;   
        case CROPPED_IMAGE:   
         Bundle extras = data.getExtras(); 
         try{ 
          if (extras != null) { 
           Bitmap myImage = extras.getParcelable("data"); 
          } 
         } 
         catch(Exception e) 
         { 
          e.printStackTrace(); 
         } 
         break; 
    

此代碼將激活您所選擇的圖像後右裁剪行動。

請注意,mImageCaptureUri是選定的圖像URI,它將通過意圖的裁剪行動。

+0

謝謝你的代碼。一點點的調整和完美的作品。 – Vinodtiru

相關問題