jayrambhia / CropperNoCropper

Instagram Style Image Cropper for Android (Library)
http://www.jayrambhia.com/project/nocropper-library
Apache License 2.0
474 stars 99 forks source link

Image is not displaying when i get image path from another activity #1

Closed alphaDroid89 closed 8 years ago

alphaDroid89 commented 8 years ago

Hi,

Thanks for sharing this library.

I'm facing a issue in the following scenario,

  1. Image path is taken from ActivityA, now i pass the path ActivityA to MainActivity(Your Cropper MainAcitivity) 2.I load the path value directly in loadNewImage(). (eg. loadNewImage(getIntent().getExtras().getString("IMAGE_PATH"))
  2. Now the image was displayed, while i perform zoom option the image was gone, when i perform touch on the gird, the actual image is displayed but i can't able to perform image zooming.
  3. But i can able to do zooming and cropping functionality by clicking Upload Image

For your ref, i added the code snipped what i had done,

public class BaseActivity extends Activity {

private static final int REQUEST_GALLERY = 21;
public static String imagePath = "";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_base);

    findViewById(R.id.image_button).setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            startGalleryIntent();
        }
    });
    findViewById(R.id.crop_button).setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            if (!imagePath.isEmpty()) {
                startActivity(new Intent(BaseActivity.this, MainActivity.class).putExtra("IMAGE_PATH", imagePath));
            }
        }
    });

}

private void startGalleryIntent() {
    Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    startActivityForResult(intent, REQUEST_GALLERY);
}

@Override
public void onActivityResult(int requestCode, int responseCode, Intent resultIntent) {
    super.onActivityResult(requestCode, responseCode, resultIntent);

    if (responseCode == RESULT_OK) {
        imagePath = BitmapUtils.getFilePathFromUri(this, resultIntent.getData());
        // loadNewImage(absPath);
    }
}

}

jayrambhia commented 8 years ago

I have done this with Camera (in one of my apps) and it has worked for me. Could you let me know the size of the bitmap that is loaded? I am currently resizing the camera image to around 1280x760 and loading in the cropper view and it seems to work.

alphaDroid89 commented 8 years ago

Image size is 1280x851; but the same image is worked fine when its selected from Cropper App, images from other activity can't able to do zoom and crop. Sorry to ask may i know, did you clear on this issue.

jayrambhia commented 8 years ago

I am trying to figure out what the issue could be. What do you mean by "images from other apps"? So to make it clear, the issue is, you are unable to zoom/crop image when you are obtaining the image from the camera. But it works fine if you select it from the gallery. Could you post some more code if possible?

alphaDroid89 commented 8 years ago

Hi, added the my working sample and video in the following link plz refer it. https://www.dropbox.com/sh/ambx6fk1x1zy0cw/AABNvPY2UaQ7XnT8ab268gHoa?dl=0

this will help me address my issue exactly.

I updated my previous comments its not Apps actually its other Activity.

jayrambhia commented 8 years ago

@SkyNite Yeah, it's a bug. Since you're loading image in onCreate(), the cropperview is not measured properly and hence there are some unforeseen issues. I'll resolve this issue in some time. Meanwhile, if you want to solve this, use a handler with delay or use a background thread (optimal solution) to load bitmap.

alphaDroid89 commented 8 years ago

Sure, I used AsyncTask for loading the image, its working fine. please let me know if good/proper solution other than this.

thanks for your quick response :)

jayrambhia commented 8 years ago

@SkyNite It turns out that this issue was related to the sample. In the sample, it was setting max zoom as 0 and hence the issue. I have updated the sample.

private void loadNewImage(String filePath) {
    mBitmap = BitmapFactory.decodeFile(filePath);
    Log.i(TAG, "bitmap: " + mBitmap.getWidth() + " " + mBitmap.getHeight());

    int maxP = Math.max(mBitmap.getWidth(), mBitmap.getHeight());
    float scale1280 = (float)maxP / 1280;

    if (mImageView.getWidth() != 0) {
        mImageView.setMaxZoom(mImageView.getWidth() * 2 / 1280f);
    } else {

        ViewTreeObserver vto = mImageView.getViewTreeObserver();
        vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
            @Override
            public boolean onPreDraw() {
                mImageView.getViewTreeObserver().removeOnPreDrawListener(this);
                mImageView.setMaxZoom(mImageView.getWidth() * 2 / 1280f);
                return true;
            }
        });

    }

    mBitmap = Bitmap.createScaledBitmap(mBitmap, (int)(mBitmap.getWidth()/scale1280),
            (int)(mBitmap.getHeight()/scale1280), true);
    mImageView.setImageBitmap(mBitmap);
}

This should fix the problem.

alphaDroid89 commented 8 years ago

@jayrambhia Great thanks yar :+1: