Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for problems with input and output File Uri with "content" schema and Android 10 SAF new requirements. #732

1 change: 0 additions & 1 deletion sample/src/main/AndroidManifest.xml
Expand Up @@ -9,7 +9,6 @@
<application
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