尝试从图库上传照片时崩溃。 并非所有图像都会导致崩溃,但很少有其他照片可以通过相机和图库成功上传。但无法理解为什么有些图像会导致崩溃。
错误日志
Unable to decode stream: java.io.FileNotFoundException:
/data/user/0/com.android.providers.downloads/cache/110826232119603-1.jpg
(Permission denied)
03-24 20:47:15.898 21129-21129/com.shareyourconnectionsyc
E/UncaughtException: java.lang.RuntimeException: Failure delivering result
ResultInfo{who=null, request=1, result=-1, data=Intent { dat=content://com.android.providers.downloads.documents/document/488 flg=0x1 }} to activity {com.shareyourconnectionsyc/com.shareyourconnectionsyc.ui.activity.EditProfileImageActivity}: java.lang.ArithmeticException: divide by zero
at android.app.ActivityThread.deliverResults(ActivityThread.java:4226)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:4269)
at android.app.ActivityThread.-wrap20(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1597)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:203)
at android.app.ActivityThread.main(ActivityThread.java:6320)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1085)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:946)
Caused by: java.lang.ArithmeticException: divide by zero
at com.shareyourconnectionsyc.ui.activity.EditProfileImageActivity.compressImage(EditProfileImageActivity.java:715)
at com.shareyourconnectionsyc.ui.activity.EditProfileImageActivity.onActivityResult(EditProfileImageActivity.java:387)
at android.app.Activity.dispatchActivityResult(Activity.java:6919)
at android.app.ActivityThread.deliverResults(ActivityThread.java:4222)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:4269)
at android.app.ActivityThread.-wrap20(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1597)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:203)
at android.app.ActivityThread.main(ActivityThread.java:6320)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1085)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:946)
这是Acitivty
public class EditProfileImageActivity extends ParentActivity implements View.OnClickListener {
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.syc_editProfilePic:
final Dialog dialog = new Dialog(context);
LayoutInflater inflater = getLayoutInflater();
View view1 = inflater.inflate(R.layout.select_image_layout, null);
dialog.setContentView(view1);
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.copyFrom(dialog.getWindow().getAttributes());
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
lp.gravity = Gravity.CENTER;
dialog.getWindow().setAttributes(lp);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
dialog.show();
galleryLayout = view1.findViewById(R.id.gallery_layout);
cameraLayout = view1.findViewById(R.id.camera_layout);
galleryLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
dialog.dismiss();
if (Build.VERSION.SDK_INT >= 23) {
if (checkPermission()) {
selectfromGallery();
// Code for above or equal 23 API Oriented Device
// Your Permission granted already .Do next code
} else {
requestPermission(); // Code for permission
}
} else {
if (isKitKat) {
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("image/*");
startActivityForResult(Intent.createChooser(intent, "Select Picture"), 1);
}
}
}
});
cameraLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
dialog.dismiss();
Intent intent1 = new Intent(context, CameraActivity.class);
startActivityForResult(intent1, 2);
}
});
break;
case R.id.finishImage:
finish();
overridePendingTransition(R.anim.slide_in_top, R.anim.slide_out_top);
break;
}
}
private void selectfromGallery() {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), 1);
}
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 2 && resultCode == RESULT_OK && data != null) {
s = data.getStringExtra("result");
Log.e("newImagePathEDIT", s);
Matrix matrix = new Matrix();
matrix.postRotate(90); // bimatp factory
BitmapFactory.Options options = new BitmapFactory.Options();
// String s = String.valueOf(fileUri);
// options.inSampleSize = 8;
final Bitmap bitmap = BitmapFactory.decodeFile(s, options);
//profileImageview.setImageBitmap(bitmap);
if (bgImage.equals("editbacghroundImage")) {
if (isNetworkAvailable()) {
new TaskChangeProfileBackgroundImage().execute();
} else {
SignUpDialog dialog = new SignUpDialog(context, "No Internet Connection", "Sorry, no Internet connectivity detected. Please reconnect and try again.");
dialog.show();
}
} else {
if (isNetworkAvailable()) {
new TaskUpdateProfileImage().execute();
} else {
SignUpDialog dialog = new SignUpDialog(context, "No Internet Connection", "Sorry, no Internet connectivity detected. Please reconnect and try again.");
dialog.show();
}
}
} else if (data != null && data.getData() != null && resultCode == RESULT_OK) {
boolean isImageFromGoogleDrive = false;
Uri uri = data.getData();
if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) {
if ("com.android.externalstorage.documents".equals(uri.getAuthority())) {
String docId = DocumentsContract.getDocumentId(uri);
String[] split = docId.split(":");
String type = split[0];
if ("primary".equalsIgnoreCase(type)) {
strImagePath = Environment.getExternalStorageDirectory() + "/" + split[1];
compressImage(strImagePath);
} else {
Pattern DIR_SEPORATOR = Pattern.compile("/");
Set<String> rv = new HashSet<>();
String rawExternalStorage = System.getenv("EXTERNAL_STORAGE");
String rawSecondaryStoragesStr = System.getenv("SECONDARY_STORAGE");
String rawEmulatedStorageTarget = System.getenv("EMULATED_STORAGE_TARGET");
if (TextUtils.isEmpty(rawEmulatedStorageTarget)) {
if (TextUtils.isEmpty(rawExternalStorage)) {
rv.add("/storage/sdcard0");
} else {
rv.add(rawExternalStorage);
}
} else {
String rawUserId;
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
rawUserId = "";
} else {
String path = Environment.getExternalStorageDirectory().getAbsolutePath();
String[] folders = DIR_SEPORATOR.split(path);
String lastFolder = folders[folders.length - 1];
boolean isDigit = false;
try {
Integer.valueOf(lastFolder);
isDigit = true;
} catch (NumberFormatException ignored) {
}
rawUserId = isDigit ? lastFolder : "";
}
if (TextUtils.isEmpty(rawUserId)) {
rv.add(rawEmulatedStorageTarget);
} else {
rv.add(rawEmulatedStorageTarget + File.separator + rawUserId);
}
}
if (!TextUtils.isEmpty(rawSecondaryStoragesStr)) {
String[] rawSecondaryStorages = rawSecondaryStoragesStr.split(File.pathSeparator);
Collections.addAll(rv, rawSecondaryStorages);
}
String[] temp = rv.toArray(new String[rv.size()]);
for (int i = 0; i < temp.length; i++) {
File tempf = new File(temp[i] + "/" + split[1]);
if (tempf.exists()) {
strImagePath = temp[i] + "/" + split[1];
compressImage(strImagePath);
}
}
}
} else if ("com.android.providers.downloads.documents".equals(uri.getAuthority())) {
String id = DocumentsContract.getDocumentId(uri);
Uri contentUri = ContentUris.withAppendedId(
Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));
Cursor cursor = null;
String column = "_data";
String[] projection = {
column
};
try {
cursor = getContentResolver().query(contentUri, projection, null, null,
null);
if (cursor != null && cursor.moveToFirst()) {
final int column_index = cursor.getColumnIndexOrThrow(column);
strImagePath = cursor.getString(column_index);
compressImage(strImagePath);
}
} finally {
if (cursor != null)
cursor.close();
}
} else if ("com.android.providers.media.documents".equals(uri.getAuthority())) {
String docId = DocumentsContract.getDocumentId(uri);
String[] split = docId.split(":");
String type = split[0];
Uri contentUri = null;
if ("image".equals(type)) {
contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
} else if ("video".equals(type)) {
contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
} else if ("audio".equals(type)) {
contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
}
String selection = "_id=?";
String[] selectionArgs = new String[]{
split[1]
};
Cursor cursor = null;
String column = "_data";
String[] projection = {
column
};
try {
cursor = getContentResolver().query(contentUri, projection, selection, selectionArgs,
null);
if (cursor != null && cursor.moveToFirst()) {
int column_index = cursor.getColumnIndexOrThrow(column);
strImagePath = cursor.getString(column_index);
compressImage(strImagePath);
}
} finally {
if (cursor != null)
cursor.close();
}
} else if ("com.google.android.apps.docs.storage".equals(uri.getAuthority())) {
isImageFromGoogleDrive = true;
}
} else if ("content".equalsIgnoreCase(uri.getScheme())) {
Cursor cursor = null;
String column = "_data";
String[] projection = {
column
};
try {
cursor = getContentResolver().query(uri, projection, null, null,
null);
if (cursor != null && cursor.moveToFirst()) {
int column_index = cursor.getColumnIndexOrThrow(column);
strImagePath = cursor.getString(column_index);
compressImage(strImagePath);
}
} finally {
if (cursor != null)
cursor.close();
}
} else if ("file".equalsIgnoreCase(uri.getScheme())) {
strImagePath = uri.getPath();
compressImage(strImagePath);
}
if (isImageFromGoogleDrive) {
try {
} catch (Exception e) {
e.printStackTrace();
}
} else {
launchUploadActivity(true);
}
}
super.onActivityResult(requestCode, resultCode, data);
}
private void launchUploadActivity(boolean b) {
s = uriSting;
Log.e("newImagePath", s);
Matrix matrix = new Matrix();
matrix.postRotate(90); // bimatp factory
BitmapFactory.Options options = new BitmapFactory.Options();
if (bgImage.equals("editbacghroundImage")) {
if (isNetworkAvailable()) {
new TaskChangeProfileBackgroundImage().execute();
} else {
SignUpDialog dialog = new SignUpDialog(context, "No Internet Connection", "Sorry, no Internet connectivity detected. Please reconnect and try again.");
dialog.show();
}
} else {
if (isNetworkAvailable()) {
new TaskUpdateProfileImage().execute();
} else {
SignUpDialog dialog = new SignUpDialog(context, "No Internet Connection", "Sorry, no Internet connectivity detected. Please reconnect and try again.");
dialog.show();
}
}
}
private void compressImage(String imageUri) {
String filePath = getRealPathFromURI(imageUri);
Bitmap scaledBitmap = null;
BitmapFactory.Options options = new BitmapFactory.Options();
// by setting this field as true, the actual bitmap pixels are not loaded in the memory. Just the bounds are loaded. If
// you try the use the bitmap here, you will get null.
options.inJustDecodeBounds = true;
Bitmap bmp = BitmapFactory.decodeFile(filePath, options);
//imageView_one.setImageBitmap(bmp);
// storeCameraPhotoInSDCard(bmp);
int actualHeight = options.outHeight;
int actualWidth = options.outWidth;
// max Height and width values of the compressed image is taken as 816x612
float maxHeight = 800.0f;//1500.0f;
float maxWidth = 600.0f;//1200.0f;
float imgRatio = actualWidth / actualHeight;
float maxRatio = maxWidth / maxHeight;
Log.e("actualHeight", String.valueOf(actualHeight));
Log.e("actualWidth", String.valueOf(actualWidth));
Log.e("imgRatio", String.valueOf(imgRatio));
Log.e("maxRatio", String.valueOf(maxRatio));
// width and height values are set maintaining the aspect ratio of the image
if (actualHeight > maxHeight || actualWidth > maxWidth) {
if (imgRatio < maxRatio) {
imgRatio = maxHeight / actualHeight;
actualWidth = (int) (imgRatio * actualWidth);
actualHeight = (int) maxHeight;
} else if (imgRatio > maxRatio) {
imgRatio = maxWidth / actualWidth;
actualHeight = (int) (imgRatio * actualHeight);
actualWidth = (int) maxWidth;
} else {
actualHeight = (int) maxHeight;
actualWidth = (int) maxWidth;
}
}
// setting inSampleSize value allows to load a scaled down version of the original image
options.inSampleSize = calculateInSampleSize(options, actualWidth, actualHeight);
// inJustDecodeBounds set to false to load the actual bitmap
options.inJustDecodeBounds = false;
// this options allow android to claim the bitmap memory if it runs low on memory
options.inPurgeable = true;
options.inInputShareable = true;
options.inTempStorage = new byte[16 * 1024];
try {
// load the bitmap from its path
bmp = BitmapFactory.decodeFile(filePath, options);
} catch (OutOfMemoryError exception) {
exception.printStackTrace();
}
try {
scaledBitmap = Bitmap.createBitmap(actualWidth, actualHeight, Bitmap.Config.ARGB_8888);
Log.e("scaledImage", String.valueOf(scaledBitmap.getByteCount()));
} catch (OutOfMemoryError exception) {
exception.printStackTrace();
}
float ratioX = actualWidth / (float) options.outWidth;
float ratioY = actualHeight / (float) options.outHeight;
float middleX = actualWidth / 2.0f;
float middleY = actualHeight / 2.0f;
Matrix scaleMatrix = new Matrix();
scaleMatrix.setScale(ratioX, ratioY, middleX, middleY);
assert scaledBitmap != null;
Canvas canvas = new Canvas(scaledBitmap);
canvas.setMatrix(scaleMatrix);
canvas.drawBitmap(bmp, middleX - bmp.getWidth() / 2, middleY - bmp.getHeight() / 2, new Paint(Paint.FILTER_BITMAP_FLAG));
// check the rotation of the image and display it properly
ExifInterface exif;
try {
exif = new ExifInterface(filePath);
int orientation = exif.getAttributeInt(
ExifInterface.TAG_ORIENTATION, 0);
Log.d("EXIF", "Exif: " + orientation);
Matrix matrix = new Matrix();
if (orientation == 6) {
matrix.postRotate(90);
Log.d("EXIF", "Exif: " + orientation);
} else if (orientation == 3) {
matrix.postRotate(180);
Log.d("EXIF", "Exif: " + orientation);
} else if (orientation == 8) {
matrix.postRotate(270);
Log.d("EXIF", "Exif: " + orientation);
}
scaledBitmap = Bitmap.createBitmap(scaledBitmap, 0, 0,
scaledBitmap.getWidth(), scaledBitmap.getHeight(), matrix,
true);
} catch (IOException e) {
e.printStackTrace();
}
FileOutputStream out = null;
String filename = getFilename();
try {
out = new FileOutputStream(filename);
// write the compressed bitmap at the destination specified by filename.
scaledBitmap.compress(Bitmap.CompressFormat.JPEG, 80, out);
Log.e("final image", String.valueOf(scaledBitmap.getByteCount()));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
private String getFilename() {
File file = new File(Environment.getExternalStorageDirectory().getPath(), "MyImage");
if (!file.exists()) {
file.mkdirs();
String newpath = file.getAbsolutePath();
// Toast.makeText(context,newpath,Toast.LENGTH_LONG).show();
}
uriSting = (file.getAbsolutePath() + "/" + System.currentTimeMillis() + ".jpg");
return uriSting;
}
private String getRealPathFromURI(String contentURI) {
Uri contentUri = Uri.parse(contentURI);
Cursor cursor = getContentResolver().query(contentUri, null, null, null, null);
if (cursor == null) {
return contentUri.getPath();
} else {
cursor.moveToFirst();
int index = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
return cursor.getString(index);
}
}
private int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
final int heightRatio = Math.round((float) height / (float) reqHeight);
final int widthRatio = Math.round((float) width / (float) reqWidth);
inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
}
final float totalPixels = width * height;
final float totalReqPixelsCap = reqWidth * reqHeight * 2;
while (totalPixels / (inSampleSize * inSampleSize) > totalReqPixelsCap) {
inSampleSize++;
}
return inSampleSize;
}
private void requestPermission() {
try {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED
|| ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED
|| ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CAMERA)) {
// Explain to the user_plachoder why we need to write the permission.
Toast.makeText(this, "This app need the permission of Camera.", Toast.LENGTH_SHORT).show();
}
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE}, PERMISSION_REQUEST_CODE);
} else {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE}, PERMISSION_REQUEST_CODE);
}
} catch (Exception e) {
e.printStackTrace();
}
}
无法理解为什么很少有图像会导致崩溃。 请帮忙。
答案 0 :(得分:0)
这是因为options.outHeight
和options.outWidth
返回0
试试这个
将Bitmap bmp = BitmapFactory.decodeFile(filePath, options);
更改为
try {
InputStream in = getContentResolver().openInputStream(
Uri.parse(path));
bmp=BitmapFactory.decodeStream(in, null, options);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
答案 1 :(得分:0)
尝试获得这样的路径:
@TargetApi(Build.VERSION_CODES.KITKAT)
public String getPath(final Uri uri) {
final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
if (isKitKat && DocumentsContract.isDocumentUri(activity, uri)) {
//Whether the Uri authority is ExternalStorageProvider.
if (com.android.externalstorage.documents".equals(uri.getAuthority()) {
final String docId = DocumentsContract.getDocumentId(uri);
final String[] split = docId.split(":");
final String type = split[0];
if ("primary".equalsIgnoreCase(type)) {
return Environment.getExternalStorageDirectory() + "/" + split[1];
}
}
// DownloadsProvider
else if ("com.android.providers.downloads.documents".equals(uri.getAuthority())) {
final String id = DocumentsContract.getDocumentId(uri);
final Uri contentUri = ContentUris.withAppendedId(
Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));
return getDataColumn(activity, contentUri, null, null);
}
// MediaProvider
else if ("com.android.providers.media.documents".equals(uri.getAuthority())) {
final String docId = DocumentsContract.getDocumentId(uri);
final String[] split = docId.split(":");
final String type = split[0];
Uri contentUri = null;
if ("image".equals(type)) {
contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
} else if ("video".equals(type)) {
contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
} else if ("audio".equals(type)) {
contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
}
final String selection = "_id=?";
final String[] selectionArgs = new String[]{
split[1]
};
return getDataColumn(activity, contentUri, selection, selectionArgs);
}
else if ("content".equalsIgnoreCase(uri.getScheme())) {
//Goole photos uri
if ("com.google.android.apps.photos.content".equals(uri.getAuthority())
return uri.getLastPathSegment();
//Goole doc uri
else if ("com.google.android.apps.docs.storage".equals(uri.getAuthority()))
return uri.getLastPathSegment();
return getRealPathFromURI(uri);
} else if ("file".equalsIgnoreCase(uri.getScheme())) {
return uri.getPath();
}
}
return null;
}
getDataColumn
是:
private String getDataColumn(Context context, Uri uri, String selection,
String[] selectionArgs) {
Cursor cursor = null;
final String column = "_data";
final String[] projection = {
column
};
try {
cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs,
null);
if (cursor != null && cursor.moveToFirst()) {
final int column_index = cursor.getColumnIndexOrThrow(column);
return cursor.getString(column_index);
} else {
return uri.getPath();
}
} finally {
if (cursor != null)
cursor.close();
}
}
getRealPathFromURI
是:
public String getRealPathFromURI(Uri contentURI) {
String result;
Cursor cursor = activity.getContentResolver().query(contentURI, null,
null, null, null);
if (cursor == null) {
result = contentURI.getPath();
} else {
cursor.moveToFirst();
try {
int idx = cursor
.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
result = cursor.getString(idx);
} catch (Exception e) {
AndyUtils
.showToast(
activity.getString(R.string.text_image_select_fail),
activity);
result = "";
}
cursor.close();
}
return result;
}
答案 2 :(得分:0)
在日志文件中明确提到“权限被拒绝”,在清单文件中声明权限,然后重试。希望这能解决您的问题。