Skip to content

Commit

Permalink
Merge pull request #732 from fabio-blanco/fix/non_native_android-10_s…
Browse files Browse the repository at this point in the history
…af-destination-file

Fix for problems with input and output File Uri with "content" schema and Android 10 SAF new requirements.
  • Loading branch information
DeMoss15 committed May 25, 2021
2 parents 32e0fcd + cd17761 commit 0ff3e6a
Show file tree
Hide file tree
Showing 20 changed files with 814 additions and 89 deletions.
1 change: 0 additions & 1 deletion sample/src/main/AndroidManifest.xml
Expand Up @@ -10,7 +10,6 @@
android:name=".SampleApp"
android:allowBackup="false"
android:icon="@mipmap/ic_launcher"
android:requestLegacyExternalStorage="true"
android:label="@string/app_name"
android:theme="@style/AppTheme">
<provider
Expand Down
44 changes: 40 additions & 4 deletions sample/src/main/java/com/yalantis/ucrop/sample/ResultActivity.java
Expand Up @@ -18,13 +18,18 @@
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.yalantis.ucrop.view.UCropView;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.channels.FileChannel;
import java.util.Calendar;
import java.util.List;
Expand Down Expand Up @@ -59,27 +64,58 @@ protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_result);
Uri uri = getIntent().getData();
int width = 0;
int height = 0;
if (uri != null) {
try {
UCropView uCropView = findViewById(R.id.ucrop);
uCropView.getCropImageView().setImageUri(uri, null);
uCropView.getOverlayView().setShowCropFrame(false);
uCropView.getOverlayView().setShowCropGrid(false);
uCropView.getOverlayView().setDimmedColor(Color.TRANSPARENT);

if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP && "content".equals(uri.getScheme())) {
TextView textViewExifWarning = findViewById(R.id.text_view_content_warning);
textViewExifWarning.setVisibility(View.VISIBLE);
}
} catch (Exception e) {
Log.e(TAG, "setImageUri", e);
Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
}

final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;

if ("content".equals(uri.getScheme())) {
InputStream is = null;
try {
is = getContentResolver().openInputStream(uri);
BitmapFactory.decodeStream(is, null, options);
} catch (FileNotFoundException e) {
Log.d(TAG, e.getMessage(), e);
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
Log.e(TAG, e.getMessage(), e);
}
}
}
} else {
File file = new File(getIntent().getData().getPath());
BitmapFactory.decodeFile(file.getAbsolutePath(), options);
}

width = options.outWidth;
height = options.outHeight;
}
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(new File(getIntent().getData().getPath()).getAbsolutePath(), options);

setSupportActionBar((Toolbar) findViewById(R.id.toolbar));
final ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setTitle(getString(R.string.format_crop_result_d_d, options.outWidth, options.outHeight));
actionBar.setTitle(getString(R.string.format_crop_result_d_d, width, height));
}
}

Expand Down

0 comments on commit 0ff3e6a

Please sign in to comment.