public static Observable < File > handleActivityResult ( final Context context , int resultCode , int requestCode , Intent data ) { if ( resultCode == Activity . RESULT_OK ) { switch ( requestCode ) { case CAPTURE_PHOTO_REQUEST_CODE : if ( mCurrentCaptureUri != null ) { revokePermissions ( context , mCurrentCaptureUri ) ; File file = getAccessibleTempFile ( context , mCurrentCaptureUri ) ; mCurrentCaptureUri = null ; return Observable . just ( file ) ; } return Observable . error ( new FileNotFoundException ( "Unable to find camera capture" ) ) ; case PICK_MEDIA_REQUEST_CODE : final Uri mediaUri = data . getData ( ) ; final String fileName = getFileName ( context , mediaUri ) ; if ( mediaUri != null ) { return Observable . fromCallable ( new Callable < File > ( ) { @ Override public File call ( ) throws Exception { ParcelFileDescriptor parcelFileDescriptor = null ; try { parcelFileDescriptor = context . getContentResolver ( ) . openFileDescriptor ( mediaUri , "r" ) ; // Now that we have the file description, get the associated input stream FileDescriptor fileDescriptor = parcelFileDescriptor . getFileDescriptor ( ) ; FileInputStream fis = new FileInputStream ( fileDescriptor ) ; FileOutputStream fos = null ; // Generate temporary output file and output file data to it try { File tempFile = createTempFile ( context , fileName ) ; fos = new FileOutputStream ( tempFile ) ; byte [ ] buffer = new byte [ 1024 ] ; int len ; while ( ( len = fis . read ( buffer ) ) > 0 ) { fos . write ( buffer , 0 , len ) ; } return tempFile ; } catch ( IOException e ) { throw OnErrorThrowable . from ( e ) ; } finally { try { parcelFileDescriptor . close ( ) ; fis . close ( ) ; if ( fos != null ) { fos . close ( ) ; } } catch ( IOException e1 ) { e1 . printStackTrace ( ) ; } } } catch ( FileNotFoundException e ) { throw OnErrorThrowable . from ( e ) ; } } } ) . compose ( RxUtils . < File > applyWorkSchedulers ( ) ) ; } return Observable . error ( new FileNotFoundException ( "Unable to load selected file" ) ) ; default : return Observable . empty ( ) ; } } return Observable . empty ( ) ; }
Handle the activity result of an intent launched to capture a photo or choose an image