Android установить изображение я обрезать в качестве обоев - PullRequest
1 голос
/ 07 января 2011

Я просто обрезаю изображение, чтобы получить его часть.Но Android установил возвращенное изображение в качестве обоев.Зачем?Я отслеживаю код Android, и в приложении Gallery3D (com.cooliris) я нашел это:

    // 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.Это говорит о том, что намерение обрезки выполнит задание по настройке.Ну, мне это совсем не нужно.Итак, как обрезать изображение без установки обоев?Спасибо!

1 Ответ

1 голос
/ 02 февраля 2012

Сделайте это в своем коде (помните, что он не будет работать на всех телефонах, таких как 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);       
          }
}
...