2021-08-06 onActivityResult在Reac

2021-08-06  本文已影响0人  无尽甜蜜里悠悠记忆

import android.app.Activity;

import android.content.Intent;

import android.net.Uri;

import android.provider.MediaStore;

import com.facebook.react.bridge.ActivityEventListener;

import com.facebook.react.bridge.Promise;

import com.facebook.react.bridge.ReactApplicationContext;

import com.facebook.react.bridge.ReactContextBaseJavaModule;

import com.facebook.react.bridge.ReactMethod;

import java.io.File;

public class ImageCropModuleextends ReactContextBaseJavaModuleimplements ActivityEventListener{

private static final String ACTION= "ImageCrop";

private static final int REQUEST_CODE_CROP_PICTURE= 0;

private Promise mPromise;

public ImageCropModule(ReactApplicationContext reactApplicationContext){

super(reactApplicationContext);

reactApplicationContext.addActivityEventListener(this);

}

@ReactMethod

    public void crop(String imagePath, Promise promise){

mPromise=promise;

Uri dest= Uri.fromFile(new File(getCurrentActivity().getCacheDir(), System.currentTimeMillis()+ "_cropped" + imagePath.substring(imagePath.lastIndexOf("."))));

CropIntentQM intent= new CropIntentQM();

intent.setImagePath(Uri.parse(imagePath));

intent.setOutputPath(dest);

        getCurrentActivity().startActivityForResult(intent.getIntent(getCurrentActivity()), REQUEST_CODE_CROP_PICTURE);

}

@Override

 public void onActivityResult(Activity activity,int requestCode,int resultCode, Intent data) {

if(requestCode==REQUEST_CODE_CROP_PICTURE){

if(resultCode== Activity.RESULT_OK){

Uri croppedUri= data.getExtras().getParcelable(MediaStore.EXTRA_OUTPUT);

mPromise.resolve(croppedUri.toString());

}else if(resultCode== Activity.RESULT_CANCELED){

mPromise.reject(String.valueOf("cancel"));

}else {

mPromise.reject(String.valueOf(404));

}

}

}

@Override

    public void onNewIntent(Intent intent) {

//

    }

@Override

    public StringgetName() {

returnACTION;

}

}

上一篇下一篇

猜你喜欢

热点阅读