0

I have a URI that contains "0/data/app/data/croppedimage.jpg" and I want to save that in internal storage with the new folder with my file name like "/storage/emulated/0/myfolder/myimage.jpg" from onActivity result.

The code is given below,

    @Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
        CropImage.ActivityResult result = CropImage.getActivityResult(data);
        if (resultCode == RESULT_OK) {
            Uri resultUri = result.getUri();}}

I have tried with FileOutputStream but myImage file crashes or broken.

code below,

 File folderPath = new File(Environment.getExternalStorageDirectory() + "/OCR");

            if (!folderPath.exists()) {
                if (folderPath.mkdir()) ; //directory is created;
            } else {
                File photo = new File("/storage/emulated/0/myfolder/myimage.jpg");
                try {
                    FileOutputStream fileOutputStream = new FileOutputStream(photo);
                    fileOutputStream.write(Integer.parseInt(resultUri.toString()));
                    fileOutputStream.flush();
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115

1 Answers1

0

I think you are using image crop library. In this case you can use FileProviders concept like this

You should append your package name to .fileprovider like com.yourpackagename.fileprovider

add this to AndroidManifest.xml

 <provider
    android:name="android.support.v4.content.FileProvider"
    android:authorities="com.yourpackagename.fileprovider"
    android:exported="false"
    android:grantUriPermissions="true">
    <meta-data
        android:name="android.support.FILE_PROVIDER_PATHS"
        android:resource="@xml/provider_path"/>
</provider>

inside res folder create xml sub folder and inside this sub folder add this file

provider_path.xml

  <?xml version="1.0" encoding="utf-8"?>
<paths>
  <external-path name="/storage/emulated/0" path="."/>
</paths>

add this as global variable inside your activity

private static final String CAPTURE_IMAGE_FILE_PROVIDER = "com.yourpackage.name.fileprovider";

And then change your startCropImageActivity() method like this

    private void startCropImageActivity(Uri imageUri)
{
    File file = null;
    try
    {
        file = createImageFile();
        Uri mUri = FileProvider.getUriForFile(this,
                CAPTURE_IMAGE_FILE_PROVIDER, file);
        CropImage.activity(imageUri)
                .setGuidelines(CropImageView.Guidelines.ON)
                .setMultiTouchEnabled(true)
                .setAllowFlipping(false)
                .setAspectRatio(1, 1)
                .setRequestedSize(350, 350)
                .setMinCropResultSize(350, 350)
                .setMaxCropResultSize(800, 800)
                .setOutputCompressFormat(Bitmap.CompressFormat.PNG)
                .setOutputCompressQuality(90)
                .setOutputUri(mUri)
                .start(this);
    }
    catch (IOException e)
    {
        e.printStackTrace();
    }
}

    private File createImageFile() throws IOException
{

    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    String imageFileName = "MYAPPNAME-" + timeStamp + ".jpg";
    File mediaStorageDir = new File(Environment.getExternalStorageDirectory(),
            "YourAppFolder");
    File storageDir = new File(mediaStorageDir + "/Profile_Images");
    if (!storageDir.exists())
    {
        storageDir.mkdirs();
    }
    File image = new File(storageDir, imageFileName);
    return image;
}

and finally

@SuppressLint("NewApi")
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{

    // handle result of pick image chooser
    if (requestCode == CropImage.PICK_IMAGE_CHOOSER_REQUEST_CODE && resultCode == Activity.RESULT_OK)
    {
        Uri imageUri = CropImage.getPickImageResultUri(this, data);

        // For API >= 23 we need to check specifically that we have permissions to read external storage.
        if (CropImage.isReadExternalStoragePermissionsRequired(this, imageUri))
        {
            // request permissions and handle the result in onRequestPermissionsResult()
            mCropImageUri = imageUri;
            requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 0);
        }
        else
        {
            // no permissions required or already grunted, can start crop image activity
            startCropImageActivity(imageUri);
        }
    }

    // handle result of CropImageActivity
    if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE)
    {
        CropImage.ActivityResult result = CropImage.getActivityResult(data);
        if (resultCode == RESULT_OK)
        {
            profileImageFilepath = result.getUri().getPath();
            Log.d("cropImageUri", result.getUri().getPath());
            new PostDataAsyncTask().execute();
            Toast.makeText(this, "Photo Selected Successfully", Toast.LENGTH_LONG).show();
        }
        else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE)
        {
            Toast.makeText(this, "Photo Selection Failed", Toast.LENGTH_SHORT).show();
        }
    }
}

take permissions for

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

This solution will save your croped image to /storage/emulated/0/yourfolder/yourimage.jpg

Mohammed Farhan
  • 1,120
  • 8
  • 14