Java 需要使用cropIwa保存图像的帮助吗

Java 需要使用cropIwa保存图像的帮助吗,java,android,Java,Android,我添加了一个目标文件,将我的图像保存在应用程序创建的文件夹中 static final File imageRoot = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), appDirectoryName); 事实上,它需要命运 以下是我所做的: cropIwaView.crop(new CropIwaSaveConfig.Builder(Uri.fromFile(im

我添加了一个目标文件,将我的图像保存在应用程序创建的文件夹中

static final File imageRoot = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), appDirectoryName);
事实上,它需要命运

以下是我所做的:

cropIwaView.crop(new CropIwaSaveConfig.Builder(Uri.fromFile(imageRoot.getAbsoluteFile()))
                .setCompressFormat(Bitmap.CompressFormat.PNG)
                .setQuality(100) //Hint for lossy compression formats
                .build());
添加@Khaled Lela

cropIwaView.crop(new CropIwaSaveConfig.Builder(getUriFromFile(this, new File(R.xml.file_paths + ".png")))
                .setCompressFormat(Bitmap.CompressFormat.PNG)
                .setQuality(100) //Hint for lossy compression formats
                .build());
添加了@Khaled Lela作为cropiwa的saveCompleteListener

cropIwaView.setCropSaveCompleteListener(new CropIwaView.CropSaveCompleteListener() {
            @Override
            public void onCroppedRegionSaved(Uri bitmapUri) {
                addPicToGallery(CropProfilePicture.this, bitmapUri);
                Toast.makeText(CropProfilePicture.this, "Done", Toast.LENGTH_SHORT).show();
                finish();
            }
        });

创建图像应保存的临时文件

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

private static File createImageFile(Context ctx) throws IOException {
    // Create an image file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",Locale.US).format(new Date());
    String imageFileName = "JPEG_" + timeStamp + "_";
    File storageDir = ctx.getExternalFilesDir(Environment.DIRECTORY_PICTURES);
    return File.createTempFile(
            imageFileName,  /* prefix */
            ".png",         /* suffix */
            storageDir      /* directory */
    );
}
  final Uri imageUri ;
    if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.LOLLIPOP)
        imageUri = Uri.fromFile(photoFile); // file://
    else
        imageUri = getUriFromFile(context,photoFile); // FileProvider
private static Uri getUriFromFile(Context context, File newFile) {
    return FileProvider.getUriForFile(context, context.getPackageName() + ".fileprovider", newFile);
}
生成
Uri
并在版本为棒棒糖或更高版本时使用FileProvide

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

private static File createImageFile(Context ctx) throws IOException {
    // Create an image file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",Locale.US).format(new Date());
    String imageFileName = "JPEG_" + timeStamp + "_";
    File storageDir = ctx.getExternalFilesDir(Environment.DIRECTORY_PICTURES);
    return File.createTempFile(
            imageFileName,  /* prefix */
            ".png",         /* suffix */
            storageDir      /* directory */
    );
}
  final Uri imageUri ;
    if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.LOLLIPOP)
        imageUri = Uri.fromFile(photoFile); // file://
    else
        imageUri = getUriFromFile(context,photoFile); // FileProvider
private static Uri getUriFromFile(Context context, File newFile) {
    return FileProvider.getUriForFile(context, context.getPackageName() + ".fileprovider", newFile);
}
FileProvider
与android版棒棒糖及更高版本一起使用

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

private static File createImageFile(Context ctx) throws IOException {
    // Create an image file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",Locale.US).format(new Date());
    String imageFileName = "JPEG_" + timeStamp + "_";
    File storageDir = ctx.getExternalFilesDir(Environment.DIRECTORY_PICTURES);
    return File.createTempFile(
            imageFileName,  /* prefix */
            ".png",         /* suffix */
            storageDir      /* directory */
    );
}
  final Uri imageUri ;
    if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.LOLLIPOP)
        imageUri = Uri.fromFile(photoFile); // file://
    else
        imageUri = getUriFromFile(context,photoFile); // FileProvider
private static Uri getUriFromFile(Context context, File newFile) {
    return FileProvider.getUriForFile(context, context.getPackageName() + ".fileprovider", newFile);
}
  • 在app res下添加文件\u path.xml

    <?xml version="1.0" encoding="utf-8"?>
      <paths>
      <external-path name="my_images" path="Android/data/com.your_package_id/files/Pictures" />
    </paths>
    
    <application
     ...// other attributes 
     >
    
    <provider
        android: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-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/file_paths" />
    </provider>
    
    </application>
    

    是的,但您需要设置文件提供程序,我将更新我的新答案。我在res下添加了文件_paths.xml,现在它位于values目录中。此外,我还遇到一个错误:必须声明元素路径\n XML文件应该在“XML”中,而不是使用名称
    XML
    res
    下创建文件夹,然后在该文件夹下创建
    文件路径
    考虑更改
    com。您的包id
    和您的应用程序包id.cropIwaView.crop(新的CropIwaSaveConfig.Builder(getUriFromFile(这是一个新文件(R.xml.File_paths+“.png”)).setCompressFormat(Bitmap.CompressFormat.png).setQuality(100)//有损压缩格式提示。build();@Khaled Lela尝试在空对象引用上调用虚拟方法“android.content.res.XmlResourceParser android.content.pm.ProviderInfo.loadXmlMetaData(android.content.pm.PackageManager,java.lang.String)”时出错