Why I get defected image from ImageCropper( android wallpaper app)?

倖福魔咒の 提交于 2019-12-13 02:23:39

问题


I am making android wallpaper app. At the start, I didn't use imagecropper and background image for android was very perfect. Then I wanted to add ImageCropper function and I noticed the problem: Image that I got from ImageCropper was very defected and bad quality. I am sure that the problem is here: (Performcrop method(starts activity for result))

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();
    }
}

OnActivityResult:

 @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);
        }
    }
    }

Here OnClick listener for SetWallpaperButton:

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();
    }
}

来源:https://stackoverflow.com/questions/32027649/why-i-get-defected-image-from-imagecropper-android-wallpaper-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!