2021-08-06 onActivityResult在React-Native使用

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;

}

}

你可能感兴趣的:(2021-08-06 onActivityResult在React-Native使用)