Skip to content Skip to sidebar Skip to footer

Need Help In Saving Images Using Cropiwa

I added a destination file to save my images in app's created folder. static final File imageRoot = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PIC

Solution 1:

Create temp file where the image should save

// Create the File where the photo should goFilephotoFile=null;
    try {
        photoFile = createImageFile(context);
    } catch (IOException ex) {
        // Error occurred while creating the File
        Timber.e("Can't create photoFile:%s",ex.getMessage());
    }

privatestatic File createImageFile(Context ctx)throws IOException {
    // Create an image file nameStringtimeStamp=newSimpleDateFormat("yyyyMMdd_HHmmss",Locale.US).format(newDate());
    StringimageFileName="JPEG_" + timeStamp + "_";
    FilestorageDir= ctx.getExternalFilesDir(Environment.DIRECTORY_PICTURES);
    return File.createTempFile(
            imageFileName,  /* prefix */".png",         /* suffix */
            storageDir      /* directory */
    );
}

Generate Uri and use FileProvide when version LOLLIPOP or above

final Uri imageUri ;
    if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.LOLLIPOP)
        imageUri = Uri.fromFile(photoFile); // file://else
        imageUri = getUriFromFile(context,photoFile); // FileProvider

Saving image logic

cropIwaView.setCropSaveCompleteListener(bitmapUri -> {
  addPicToGallery(context, bitmapUri); // sendBroadcast to gallery to scan new added images...
 });

cropIwaView.crop(new CropIwaSaveConfig.Builder(imageUri)
                .setCompressFormat(Bitmap.CompressFormat.PNG)
                .setQuality(100) //Hint for lossy compression formats.build());

Use FileProvider with android version LOLLIPOP and above

privatestaticUrigetUriFromFile(Context context, File newFile) {
    returnFileProvider.getUriForFile(context, context.getPackageName() + ".fileprovider", newFile);
}
  • Under app res add file_paths.xml

    <?xml version="1.0" encoding="utf-8"?><paths><external-pathname="my_images"path="Android/data/com.your_package_id/files/Pictures" /></paths>
  • manifest.xml

    <application...// otherattributes 
     ><providerandroid:name="android.support.v4.content.FileProvider"android:authorities="com.your_package_id.fileprovider"android:readPermission="com.your_package_id.fileprovider.READ"android:grantUriPermissions="true"android:exported="false"><meta-dataandroid:name="android.support.FILE_PROVIDER_PATHS"android:resource="@xml/file_paths" /></provider></application>
    • add permission

      <uses-permissionandroid:name="android.permission.WRITE_EXTERNAL_STORAGE"/><uses-permissionandroid:name="android.permission.READ_EXTERNAL_STORAGE" />

Update Update image on gallery.

privatestaticvoidaddPicToGallery(Context context, Uri contentUri) {
    IntentmediaScanIntent=newIntent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
    mediaScanIntent.setData(contentUri);
    context.sendBroadcast(mediaScanIntent);
}

Post a Comment for "Need Help In Saving Images Using Cropiwa"