Fixes write permission for each sdk version, improved image loading

This commit is contained in:
Olga Miller 2017-01-06 12:32:31 +01:00
parent c14af42222
commit fbd4aef064
4 changed files with 46 additions and 31 deletions

View file

@ -3,8 +3,7 @@
package="om.sstvencoder"> package="om.sstvencoder">
<uses-permission <uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE" android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
android:maxSdkVersion="18"/>
<uses-feature <uses-feature
android:name="android.hardware.camera" android:name="android.hardware.camera"
android:required="false"/> android:required="false"/>

View file

@ -177,15 +177,18 @@ public class CropView extends ImageView {
mImageOK = false; mImageOK = false;
mOrientation = 0; mOrientation = 0;
recycle(); recycle();
invalidate(); invalidate();
} }
public void setBitmapStream(InputStream stream) throws IOException { public void setBitmap(@NonNull InputStream stream) throws IOException {
mImageOK = false; mImageOK = false;
mOrientation = 0; mOrientation = 0;
recycle(); recycle();
loadImage(stream);
invalidate();
}
private void loadImage(InputStream stream) throws IOException {
// app6 + exif // app6 + exif
int bufferBytes = 1048576; int bufferBytes = 1048576;
if (!stream.markSupported()) if (!stream.markSupported())
@ -214,7 +217,6 @@ public class CropView extends ImageView {
mImageOK = true; mImageOK = true;
resetInputRect(); resetInputRect();
invalidate();
} }
private void recycle() { private void recycle() {

View file

@ -42,7 +42,7 @@ import android.view.SubMenu;
import android.widget.Toast; import android.widget.Toast;
import java.io.File; import java.io.File;
import java.io.FileNotFoundException; import java.io.InputStream;
import om.sstvencoder.ModeInterfaces.IModeInfo; import om.sstvencoder.ModeInterfaces.IModeInfo;
import om.sstvencoder.TextOverlay.Label; import om.sstvencoder.TextOverlay.Label;
@ -86,8 +86,7 @@ public class MainActivity extends AppCompatActivity {
uri = mSettings.getImageUri(); uri = mSettings.getImageUri();
verbose = false; verbose = false;
} }
if (loadImage(uri, verbose)) loadImage(uri, verbose);
mSettings.setImageUri(uri);
} }
private Uri getImageUriFromIntent(Intent intent) { private Uri getImageUriFromIntent(Intent intent) {
@ -105,29 +104,39 @@ public class MainActivity extends AppCompatActivity {
// Set verbose to false for any Uri that might have expired (e.g. shared from browser). // Set verbose to false for any Uri that might have expired (e.g. shared from browser).
private boolean loadImage(Uri uri, boolean verbose) { private boolean loadImage(Uri uri, boolean verbose) {
ContentResolver resolver = getContentResolver();
InputStream stream = null;
if (uri != null) { if (uri != null) {
mSettings.setImageUri(uri);
try { try {
ContentResolver resolver = getContentResolver(); stream = resolver.openInputStream(uri);
mCropView.setBitmapStream(resolver.openInputStream(uri)); } catch (Exception ex) { // e.g. FileNotFoundException, SecurityException
mCropView.rotateImage(getOrientation(resolver, uri));
return true;
} catch (FileNotFoundException ex) {
if (ex.getCause() instanceof ErrnoException if (ex.getCause() instanceof ErrnoException
&& ((ErrnoException) ex.getCause()).errno == OsConstants.EACCES) { && ((ErrnoException) ex.getCause()).errno == OsConstants.EACCES) {
requestPermissions(); requestPermissions();
} else if (verbose) { return false;
String s = getString(R.string.load_img_err_title) + ": \n" + ex.getMessage();
Toast.makeText(this, s, Toast.LENGTH_LONG).show();
}
} catch (Exception ex) {
if (verbose) {
String s = Utility.createMessage(ex) + "\n\n" + uri;
showErrorMessage(getString(R.string.load_img_err_title), ex.getMessage(), s);
} }
showFileNotLoadedMessage(ex, verbose);
} }
} }
mCropView.setNoBitmap(); if (stream == null) {
return false; mCropView.setNoBitmap();
mSettings.setImageUri(null);
return false;
}
return loadImage(stream, resolver, uri);
}
private boolean loadImage(InputStream stream, ContentResolver resolver, Uri uri) {
try {
mCropView.setBitmap(stream);
} catch (Exception ex) {
String s = Utility.createMessage(ex) + "\n\n" + uri;
showErrorMessage(getString(R.string.load_img_err_title), ex.getMessage(), s);
return false;
}
mCropView.rotateImage(getOrientation(resolver, uri));
return true;
} }
private boolean isIntentActionValid(String action) { private boolean isIntentActionValid(String action) {
@ -147,6 +156,15 @@ public class MainActivity extends AppCompatActivity {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 1); ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 1);
} }
private void showFileNotLoadedMessage(Exception ex, boolean verbose) {
String s;
if (verbose)
s = getString(R.string.load_img_err_title) + ": \n" + ex.getMessage();
else
s = getString(R.string.message_prev_img_not_loaded);
Toast.makeText(this, s, Toast.LENGTH_LONG).show();
}
private void showErrorMessage(final String title, final String shortText, final String longText) { private void showErrorMessage(final String title, final String shortText, final String longText) {
AlertDialog.Builder builder = new AlertDialog.Builder(this); AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(title); builder.setTitle(title);
@ -280,18 +298,13 @@ public class MainActivity extends AppCompatActivity {
case REQUEST_IMAGE_CAPTURE: case REQUEST_IMAGE_CAPTURE:
if (resultCode == RESULT_OK) { if (resultCode == RESULT_OK) {
Uri uri = Uri.fromFile(mFile); Uri uri = Uri.fromFile(mFile);
if (loadImage(uri, true)) { if (loadImage(uri, true))
mSettings.setImageUri(uri);
addImageToGallery(uri); addImageToGallery(uri);
}
} }
break; break;
case REQUEST_PICK_IMAGE: case REQUEST_PICK_IMAGE:
if (resultCode == RESULT_OK && data != null) { if (resultCode == RESULT_OK && data != null)
Uri uri = data.getData(); loadImage(data.getData(), true);
if (loadImage(uri, true))
mSettings.setImageUri(uri);
}
break; break;
default: default:
super.onActivityResult(requestCode, resultCode, data); super.onActivityResult(requestCode, resultCode, data);

View file

@ -27,6 +27,7 @@
<string name="load_img_err_title">Image loading error</string> <string name="load_img_err_title">Image loading error</string>
<string name="load_img_orientation_err_title">Image orientation error</string> <string name="load_img_orientation_err_title">Image orientation error</string>
<string name="load_img_err_txt_unsupported">Unsupported content.</string> <string name="load_img_err_txt_unsupported">Unsupported content.</string>
<string name="message_prev_img_not_loaded">Previous image could not be loaded.</string>
<string name="btn_send_email">Send Email</string> <string name="btn_send_email">Send Email</string>
<string name="btn_ok">OK</string> <string name="btn_ok">OK</string>
<string name="email_subject">SSTV Encoder - Bug Report</string> <string name="email_subject">SSTV Encoder - Bug Report</string>