2011-01-07 200 views
1

我只是裁剪圖像來獲取它的一部分。但Android將返回的圖像設置爲壁紙。爲什麼?我跟蹤了Android的代碼,並在Gallery3D應用(com.cooliris),我發現這一點:android設置圖像我作爲壁紙

// TODO: A temporary file is NOT necessary 
    // The CropImage intent should be able to set the wallpaper directly 
    // without writing to a file, which we then need to read here to write 
    // it again as the final wallpaper, this is silly 
    mTempFile = getFileStreamPath("temp-wallpaper"); 
    mTempFile.getParentFile().mkdirs(); 

    int width = getWallpaperDesiredMinimumWidth(); 
    int height = getWallpaperDesiredMinimumHeight(); 
    intent.putExtra("outputX", width); 
    intent.putExtra("outputY", height); 
    intent.putExtra("aspectX", width); 
    intent.putExtra("aspectY", height); 
    intent.putExtra("scale", true); 
    intent.putExtra("noFaceDetection", true); 
    intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(mTempFile)); 
    intent.putExtra("outputFormat", Bitmap.CompressFormat.PNG.name()); 
    // TODO: we should have an extra called "setWallpaper" to ask CropImage 
    // to set the cropped image as a wallpaper directly. This means the 
    // SetWallpaperThread should be moved out of this class to CropImage 

請重點關注的最後一行,在TODO。它告訴莊稼意圖將做設定工作。好吧,我根本不需要它。那麼,如何在沒有設置WALLPAPER的情況下製作圖像?謝謝!

回答

1

在你的代碼(記住,因爲他們使用自己的畫廊/攝像頭也不會在所有手機上運行,​​像HTC執行此操作。

File f = new File(Environment.getExternalStorageDirectory(), "/temporary_holder.png"); 
f.createNewFile(); 
Intent intent = new Intent("com.android.camera.action.CROP"); 
intent.setClassName("com.android.gallery", "com.android.camera.CropImage"); 
intent.setType("image/*"); 
intent.setData(ImageToSetUri); // Local URI of your image 
intent.putExtra("crop", true); 
intent.putExtra("outputX", width); // width/height of your image 
intent.putExtra("outputY", height); 
intent.putExtra("aspectX", width); 
intent.putExtra("aspectY", height); 
intent.putExtra("scale", true); 
intent.putExtra("noFaceDetection", true); 
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f)); 
intent.putExtra("outputFormat", Bitmap.CompressFormat.PNG.name()); 
startActivityForResult(intent, 1); 

那麼這樣做是爲了獲取圖像作爲位圖,或者你想要

public void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 
    if (resultCode == Activity.RESULT_OK) 
      if (data != null) { 
       Bitmap selectedImage = BitmapFactory.decodeFile(f);  
      } 
}