StartActivityForResult not working in AsyncTask

放肆的年华 提交于 2019-12-13 05:10:05

问题


I am trying to use Biokys crop image library within my AsyncTask class and for some reason when trying to start the crop image class for a result, I get an error.

I have looked up what would be causing this and people have suggested that you should use:

YourClassName.this.startActivityForResult(Intent, RESULT_CODE);

But this isn't working for me.

If someone would be able to explain why this is happening, would be much appreciated.

Class:

    private void runCropImage() {


    Intent intent = new Intent(context, CropImage.class);

    // tell CropImage activity to look for image to crop 
    Bitmap filePath = bmImg;
    intent.putExtra(CropImage.IMAGE_PATH, filePath);

    // allow CropImage activity to rescale image
    intent.putExtra(CropImage.SCALE, true);

    // if the aspect ratio is fixed to ratio 3/2
    intent.putExtra(CropImage.ASPECT_X, 3);
    intent.putExtra(CropImage.ASPECT_Y, 2);

    // start activity CropImage with certain request code and listen
    // for result
    SetWallpaperAsync.this.startActivityForResult(intent, REQUEST_CODE_CROP_IMAGE);

}

回答1:


Instead of SetWallpaperAsync.this use (Activity) context like this:

    ((Activity) context).startActivityForResult(intent, REQUEST_CODE_CROP_IMAGE);



回答2:


Have you tried replacing

 Intent intent = new Intent(context, CropImage.class);

with

 Intent intent = new Intent(YourClassName.this, CropImage.class);

?



来源:https://stackoverflow.com/questions/20656937/startactivityforresult-not-working-in-asynctask

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