显示图库中的图片时,应用程序崩溃
When displaying a picture from the gallery, the application breaks down
对不起我的英语。
应用程序运行正常。将文件附加到 imageview,然后将其发送到服务器。
现在,在选择图片(文件)后,应用程序将其显示在图像视图中,所有方法都正确运行并结束。
但是应用程序将我抛到上一个片段,日志中没有任何错误。
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == Activity.RESULT_OK && requestCode == 100){
if(getPath(data.getData()) != null){
Uri fileUri = data.getData();
file = new File(getPath(fileUri));
String mimetype = getActivity().getContentResolver().getType(fileUri);
Log.d("mylog","name " + file.getName() + " type " + mimetype);
imageV.setVisibility(View.VISIBLE);
imageV.setImageResource(R.drawable.icons8highpriority96);
if(mimetype.equals("image/jpeg")){ imageV.setImageURI(fileUri);}
if(mimetype.equals("image/png")){ imageV.setImageURI(fileUri);}
if(mimetype.equals("application/pdf")){ imageV.setImageResource(R.drawable.icons8pdf80);}
if(mimetype.equals("text/html")){ imageV.setImageResource(R.drawable.icons8doc80);}
textFileName.setText(file.getName());
textFileName.setVisibility(View.VISIBLE);
}else {
imageV.setVisibility(View.VISIBLE);
textFileName.setVisibility(View.VISIBLE);
textFileName.setText("Выберите файл из определенного раздела! ");}
}
}
我附上一些让我困惑的日志 -
447-705/system_process I/ActivityManager: START u0
{act=android.intent.action.GET_CONTENT typ=*/*
cmp=com.android.gallery/com.android.camera.ImageGallery} from pid
1382
201-304/? W/genymotion_audio: out_write() limiting sleep time 34149
to 23219
201-304/? W/genymotion_audio: out_write() limiting sleep time 45759
to 23219
201-304/? W/genymotion_audio: out_write() limiting sleep time 37369
to 23219
447-705/system_process D/dalvikvm: GC_FOR_ALLOC freed 616K, 27% free
6910K/9424K, paused 5ms, total 5ms
447-705/system_process D/dalvikvm: GC_FOR_ALLOC freed 755K, 31% free
6526K/9424K, paused 8ms, total 8ms
447-544/system_process D/MobileDataStateTracker: default:
setPolicyDataEnable(enabled=true)
201-304/? W/genymotion_audio: out_write() limiting sleep time 28979
to 23219
447-540/system_process I/qtaguid: Failed write_ctrl(s 1 10016)
res=-1
errno=1
447-540/system_process W/NetworkManagementSocketTagger:
setKernelCountSet(10016, 1) failed with errno -1
1448-1448/com.android.gallery D/dalvikvm: GC_FOR_ALLOC freed 662K,
27% free
3596K/4900K, paused 2ms, total 2ms
1448-1448/com.android.gallery D/dalvikvm: GC_FOR_ALLOC freed 527K,
28% free
3547K/4900K, paused 1ms, total 1ms
201-304/? W/genymotion_audio: out_write() limiting sleep time 31767
to 23219
1448-1448/com.android.gallery D/dalvikvm: GC_FOR_ALLOC freed 220K,
27% free
3613K/4900K, paused 1ms, total 1ms
447-544/system_process D/MobileDataStateTracker: default:
setPolicyDataEnable(enabled=true)
447-464/system_process I/ActivityManager: Displayed
com.android.gallery/com.android.camera.ImageGallery: +124ms
1448-1448/com.android.gallery D/dalvikvm: GC_FOR_ALLOC freed 103K,
21% free
3915K/4900K, paused 2ms, total 2ms
1448-1448/com.android.gallery D/dalvikvm: GC_FOR_ALLOC freed 265K,
16% free
4121K/4900K, paused 18ms, total 18ms
1448-1448/com.android.gallery D/dalvikvm: GC_FOR_ALLOC freed 586K,
24% free
3748K/4900K, paused 3ms, total 3ms
447-544/system_process D/MobileDataStateTracker: default:
setPolicyDataEnable(enabled=true)
447-544/system_process D/MobileDataStateTracker: default:
setPolicyDataEnable(enabled=true)
447-540/system_process I/qtaguid: Failed write_ctrl(s 0 10053)
res=-1
errno=1
447-540/system_process W/NetworkManagementSocketTagger:
setKernelCountSet(10053, 0) failed with errno -1
447-544/system_process D/MobileDataStateTracker: default:
447-539/system_process D/dalvikvm: GC_FOR_ALLOC freed 999K, 31% free
6545K/9424K, paused 4ms, total 5ms
447-544/system_process D/MobileDataStateTracker: default:
setPolicyDataEnable(enabled=true)
447-544/system_process D/MobileDataStateTracker: default:
setPolicyDataEnable(enabled=true)
201-304/? W/genymotion_audio: out_write() limiting sleep time 44149
to 23219
201-304/? W/genymotion_audio: out_write() limiting sleep time 55759
to 23219
447-447/system_process D/dalvikvm: GC_FOR_ALLOC freed 444K, 27% free
6903K/9424K, paused 6ms, total 6ms
447-447/system_process D/dalvikvm: GC_FOR_ALLOC freed 749K, 31% free
6525K/9424K, paused 5ms, total 5ms
447-544/system_process D/MobileDataStateTracker: default:
setPolicyDataEnable(enabled=true)
447-540/system_process I/qtaguid: Failed write_ctrl(s 1 10053)
res=-1
errno=1
447-540/system_process W/NetworkManagementSocketTagger:
setKernelCountSet(10053, 1) failed with errno -1
1382-1382/com.example.usersad.myapplication D/mylog: name
IMG_20180327_045230.jpg type image/jpeg
我将我的代码复制到一个新项目中并且它有效!
来自 activi 和 fragment。
但在与画廊互动后的主项目中,该片段已关闭。
用 try
和 cath
包装你写在 onActivityResult
中的代码并打印异常
我建议你逐行调试你的代码,我认为有些东西是空的。有时在不同的 sdk 中,get Path(file Uri) 是不同的
你可以按照这个结构。用你的变量和方法替换变量和方法。希望它能帮助你。请评论任何查询。
if (requestCode == SELECT_GALLERY && resultCode == RESULT_OK && null != data) {
try {
galleryopen = false;
Uri selectedImage = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
Bitmap bitmap = BitmapFactory.decodeFile(picturePath);
Consts.img_Bitmap = null;
Consts.img_Bitmap = BitmapFactory.decodeFile(picturePath);
Intent intent = new Intent(MainActivity.this, CropActivity.class);
} catch (Exception e) {
e.printStackTrace();
}
} else if (requestCode == SELECT_CAMERA) {
try {
galleryopen = false;
Uri selectedImageUri = getImageUri();
String uri = selectedImageUri.toString();
Consts.img_Bitmap = null;
Consts.img_Bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), selectedImageUri);
ExifInterface ei = new ExifInterface(selectedImageUri.getPath());
int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
Consts.img_Bitmap = rotateImage(Consts.img_Bitmap, 90);
break;
case ExifInterface.ORIENTATION_ROTATE_180:
Consts.img_Bitmap = rotateImage(Consts.img_Bitmap, 180);
break;
case ExifInterface.ORIENTATION_ROTATE_270:
Consts.img_Bitmap = rotateImage(Consts.img_Bitmap, 270);
break;
default:
break;
}
Intent intent = new Intent(MainActivity.this, CropActivity.class);
startActivity(intent);
} catch (Exception e) {
galleryopen = false;
e.printStackTrace();
}
} else {
galleryopen = false;
Toast.makeText(MainActivity.this, "you have not selected any Image", Toast.LENGTH_SHORT).show();
}
尝试如下所述编辑 AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="tscolari.photo"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="10" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".PhotoFilterActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
我发现了我的错误。
该应用程序已关闭,因为我在主 activity.
的 onStart 方法中调用了另一个片段
对不起我的英语。
应用程序运行正常。将文件附加到 imageview,然后将其发送到服务器。
现在,在选择图片(文件)后,应用程序将其显示在图像视图中,所有方法都正确运行并结束。
但是应用程序将我抛到上一个片段,日志中没有任何错误。
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == Activity.RESULT_OK && requestCode == 100){
if(getPath(data.getData()) != null){
Uri fileUri = data.getData();
file = new File(getPath(fileUri));
String mimetype = getActivity().getContentResolver().getType(fileUri);
Log.d("mylog","name " + file.getName() + " type " + mimetype);
imageV.setVisibility(View.VISIBLE);
imageV.setImageResource(R.drawable.icons8highpriority96);
if(mimetype.equals("image/jpeg")){ imageV.setImageURI(fileUri);}
if(mimetype.equals("image/png")){ imageV.setImageURI(fileUri);}
if(mimetype.equals("application/pdf")){ imageV.setImageResource(R.drawable.icons8pdf80);}
if(mimetype.equals("text/html")){ imageV.setImageResource(R.drawable.icons8doc80);}
textFileName.setText(file.getName());
textFileName.setVisibility(View.VISIBLE);
}else {
imageV.setVisibility(View.VISIBLE);
textFileName.setVisibility(View.VISIBLE);
textFileName.setText("Выберите файл из определенного раздела! ");}
}
}
我附上一些让我困惑的日志 -
447-705/system_process I/ActivityManager: START u0
{act=android.intent.action.GET_CONTENT typ=*/*
cmp=com.android.gallery/com.android.camera.ImageGallery} from pid
1382
201-304/? W/genymotion_audio: out_write() limiting sleep time 34149
to 23219
201-304/? W/genymotion_audio: out_write() limiting sleep time 45759
to 23219
201-304/? W/genymotion_audio: out_write() limiting sleep time 37369
to 23219
447-705/system_process D/dalvikvm: GC_FOR_ALLOC freed 616K, 27% free
6910K/9424K, paused 5ms, total 5ms
447-705/system_process D/dalvikvm: GC_FOR_ALLOC freed 755K, 31% free
6526K/9424K, paused 8ms, total 8ms
447-544/system_process D/MobileDataStateTracker: default:
setPolicyDataEnable(enabled=true)
201-304/? W/genymotion_audio: out_write() limiting sleep time 28979
to 23219
447-540/system_process I/qtaguid: Failed write_ctrl(s 1 10016)
res=-1
errno=1
447-540/system_process W/NetworkManagementSocketTagger:
setKernelCountSet(10016, 1) failed with errno -1
1448-1448/com.android.gallery D/dalvikvm: GC_FOR_ALLOC freed 662K,
27% free
3596K/4900K, paused 2ms, total 2ms
1448-1448/com.android.gallery D/dalvikvm: GC_FOR_ALLOC freed 527K,
28% free
3547K/4900K, paused 1ms, total 1ms
201-304/? W/genymotion_audio: out_write() limiting sleep time 31767
to 23219
1448-1448/com.android.gallery D/dalvikvm: GC_FOR_ALLOC freed 220K,
27% free
3613K/4900K, paused 1ms, total 1ms
447-544/system_process D/MobileDataStateTracker: default:
setPolicyDataEnable(enabled=true)
447-464/system_process I/ActivityManager: Displayed
com.android.gallery/com.android.camera.ImageGallery: +124ms
1448-1448/com.android.gallery D/dalvikvm: GC_FOR_ALLOC freed 103K,
21% free
3915K/4900K, paused 2ms, total 2ms
1448-1448/com.android.gallery D/dalvikvm: GC_FOR_ALLOC freed 265K,
16% free
4121K/4900K, paused 18ms, total 18ms
1448-1448/com.android.gallery D/dalvikvm: GC_FOR_ALLOC freed 586K,
24% free
3748K/4900K, paused 3ms, total 3ms
447-544/system_process D/MobileDataStateTracker: default:
setPolicyDataEnable(enabled=true)
447-544/system_process D/MobileDataStateTracker: default:
setPolicyDataEnable(enabled=true)
447-540/system_process I/qtaguid: Failed write_ctrl(s 0 10053)
res=-1
errno=1
447-540/system_process W/NetworkManagementSocketTagger:
setKernelCountSet(10053, 0) failed with errno -1
447-544/system_process D/MobileDataStateTracker: default:
447-539/system_process D/dalvikvm: GC_FOR_ALLOC freed 999K, 31% free
6545K/9424K, paused 4ms, total 5ms
447-544/system_process D/MobileDataStateTracker: default:
setPolicyDataEnable(enabled=true)
447-544/system_process D/MobileDataStateTracker: default:
setPolicyDataEnable(enabled=true)
201-304/? W/genymotion_audio: out_write() limiting sleep time 44149
to 23219
201-304/? W/genymotion_audio: out_write() limiting sleep time 55759
to 23219
447-447/system_process D/dalvikvm: GC_FOR_ALLOC freed 444K, 27% free
6903K/9424K, paused 6ms, total 6ms
447-447/system_process D/dalvikvm: GC_FOR_ALLOC freed 749K, 31% free
6525K/9424K, paused 5ms, total 5ms
447-544/system_process D/MobileDataStateTracker: default:
setPolicyDataEnable(enabled=true)
447-540/system_process I/qtaguid: Failed write_ctrl(s 1 10053)
res=-1
errno=1
447-540/system_process W/NetworkManagementSocketTagger:
setKernelCountSet(10053, 1) failed with errno -1
1382-1382/com.example.usersad.myapplication D/mylog: name
IMG_20180327_045230.jpg type image/jpeg
我将我的代码复制到一个新项目中并且它有效! 来自 activi 和 fragment。 但在与画廊互动后的主项目中,该片段已关闭。
用 try
和 cath
包装你写在 onActivityResult
中的代码并打印异常
我建议你逐行调试你的代码,我认为有些东西是空的。有时在不同的 sdk 中,get Path(file Uri) 是不同的
你可以按照这个结构。用你的变量和方法替换变量和方法。希望它能帮助你。请评论任何查询。
if (requestCode == SELECT_GALLERY && resultCode == RESULT_OK && null != data) {
try {
galleryopen = false;
Uri selectedImage = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
Bitmap bitmap = BitmapFactory.decodeFile(picturePath);
Consts.img_Bitmap = null;
Consts.img_Bitmap = BitmapFactory.decodeFile(picturePath);
Intent intent = new Intent(MainActivity.this, CropActivity.class);
} catch (Exception e) {
e.printStackTrace();
}
} else if (requestCode == SELECT_CAMERA) {
try {
galleryopen = false;
Uri selectedImageUri = getImageUri();
String uri = selectedImageUri.toString();
Consts.img_Bitmap = null;
Consts.img_Bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), selectedImageUri);
ExifInterface ei = new ExifInterface(selectedImageUri.getPath());
int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
Consts.img_Bitmap = rotateImage(Consts.img_Bitmap, 90);
break;
case ExifInterface.ORIENTATION_ROTATE_180:
Consts.img_Bitmap = rotateImage(Consts.img_Bitmap, 180);
break;
case ExifInterface.ORIENTATION_ROTATE_270:
Consts.img_Bitmap = rotateImage(Consts.img_Bitmap, 270);
break;
default:
break;
}
Intent intent = new Intent(MainActivity.this, CropActivity.class);
startActivity(intent);
} catch (Exception e) {
galleryopen = false;
e.printStackTrace();
}
} else {
galleryopen = false;
Toast.makeText(MainActivity.this, "you have not selected any Image", Toast.LENGTH_SHORT).show();
}
尝试如下所述编辑 AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="tscolari.photo"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="10" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".PhotoFilterActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
我发现了我的错误。 该应用程序已关闭,因为我在主 activity.
的 onStart 方法中调用了另一个片段