1

我正在制作安卓壁纸应用程序。一开始我没有使用imagecropper,android的背景图像非常完美。然后我想添加 ImageCropper 功能,我注意到了问题:我从 ImageCropper 获得的图像非常有缺陷,质量很差。我确定问题出在此处:(Performcrop 方法(为结果启动活动))

public void performCrop(Uri picUri, Bitmap qq){
    try {
        utils = new Utils(getApplicationContext());
        int height, width;
        Display display = getWindowManager().getDefaultDisplay();
        Point size = new Point();
        display.getSize(size);
        height = size.y;
        width = size.x;

        File f= new File(utils.saveImageToSDCard(qq));
        f.createNewFile();
        Intent cropIntent = new Intent("com.android.camera.action.CROP");
        //indicate image type and Uri
        cropIntent.setDataAndType(picUri, "image/*");
        //set crop properties
        cropIntent.putExtra("crop", "true");

        //indicate aspect of desired crop
        cropIntent.putExtra("aspectX", 1);
        cropIntent.putExtra("aspectY", 1);
        //indicate  X and Y
        cropIntent.putExtra("outputX", width);
        cropIntent.putExtra("outputY", height);
        //retrieve data on return
        cropIntent.putExtra("return-data", true);
        //start the activity - we handle returning in onActivityResult
        cropIntent.putExtra(MediaStore.EXTRA_OUTPUT, picUri.fromFile(f));
        startActivityForResult(cropIntent, 1);

    }
    catch(ActivityNotFoundException anfe){
        //display an error message
        String errorMessage = "Whoops - your device doesn't support the crop action!";
        Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT);
        toast.show();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

活动结果:

 @Override
protected void onActivityResult(int requestCode,int resultCode, Intent data){

super.onActivityResult(requestCode, resultCode, data);

    if(requestCode == 1) {

//get the cropped bitmap
        if (data != null) {
            Bundle extras = data.getExtras();
            int height, width;
            Display display = getWindowManager().getDefaultDisplay();
            Point size = new Point();
            display.getSize(size);
            height = size.y;
            width = size.x;
            utils = new Utils(getApplicationContext());
            Bitmap thePic = (Bitmap) (extras.getParcelable("data"));
            Bitmap krutoi = Bitmap.createBitmap(thePic, 0, 0, width, height);
            utils.setAsWallpaper(krutoi);
        }
    }
    }

这里 SetWallpaperButton 的 OnClick 监听器:

llDownloadWallpaper.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            utils = new Utils(getApplicationContext());
            bitmap = ((BitmapDrawable) fullImageView.getDrawable())
                    .getBitmap();
            utils.saveImageToSDCard(bitmap);
        }
    });

saveImageToSdCard(Utils.java):

public String saveImageToSDCard(Bitmap bitmap) {
    File myDir = new File(
            Environment
                    .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
            pref.getGalleryName());

    myDir.mkdirs();
    Random generator = new Random();
    int n = 10000;
    n = generator.nextInt(n);
    String fname = "Wallpaper-" + n + ".jpg";
    File file = new File(myDir, fname);
    if (file.exists())
        file.delete();
    try {
        FileOutputStream out = new FileOutputStream(file);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
        out.flush();
        out.close();
        Toast.makeText(
                _context,
                _context.getString(R.string.toast_saved).replace("#",
                        "\"" + pref.getGalleryName() + "\""),
                Toast.LENGTH_SHORT).show();
        Log.d(TAG, "Wallpaper saved to: " + file.getAbsolutePath());

    } catch (Exception e) {
        e.printStackTrace();
        Toast.makeText(_context,
                _context.getString(R.string.toast_saved_failed),
                Toast.LENGTH_SHORT).show();
    }
    return file.getAbsolutePath();
}

setAsWallpaper(Utils.java):

public void setAsWallpaper(Bitmap bitmap) {
    try {
        WallpaperManager wm = WallpaperManager.getInstance(_context);

        wm.setBitmap(bitmap);
        Toast.makeText(_context,
                _context.getString(R.string.toast_wallpaper_set),
                Toast.LENGTH_SHORT).show();
    } catch (Exception e) {
        e.printStackTrace();
        Toast.makeText(_context,
                _context.getString(R.string.toast_wallpaper_set_failed),
                Toast.LENGTH_SHORT).show();
    }
}
4

0 回答 0