问题描述
我试图用相机类我的应用程序。我只是想点击的图片,并设置在ImageView的,然后在某些URL张贴。发布的URL工作正常,但同时点击任何图片和恢复回到同一活动从那里我导航到相机应用的某个时候会出现问题。它工作正常,在HTC野火(2.2版),但有时给人的例外(失败几率1/25),但是当我测试它在索尼Xperia米罗或三星标签(4.0版),它提供了相同的异常多次(失败几率20/25) 。我没有收到哪里出了问题的存在,因为有时应用程序运行顺利,没有任何异常,但与4.0或以上版本,它的力量关闭很多次,但有时它工作得很好。
例外是:了java.lang.RuntimeException:无法恢复活动{fable.eventippo / fable.eventippo.EventsIppoPaymentActivity}:java.lang.RuntimeException的:不提供结果ResultInfo {谁= tabHome,请求= 1,结果= -1,数据=意向{DAT =内容://媒体/外部/图片/媒体/ 17271}}到活动{fable.eventippo / fable.eventippo.EventsIppoPaymentActivity}: java.lang.ClassCastException:fable.eventippo.Home不能转换为fable.eventippo.AddEvent
完成code在这里给出。
按钮的onclick。
浏览=(按钮)findViewById(R.id.browse);
browse.setOnClickListener(新View.OnClickListener(){
@覆盖
公共无效的onClick(视图v){
最后的CharSequence []项目= {摄像头,画廊};
AlertDialog.Builder建设者=新AlertDialog.Builder(
的getParent());
builder.setTitle(浏览自);
builder.setItems(项目,新DialogInterface.OnClickListener(){
公共无效的onClick(DialogInterface对话,诠释项){
如果(项目[项目] ==照相机){
PackageManager下午= getPackageManager();
如果(pm.hasSystemFeature(PackageManager.FEATURE_CAMERA)){
意图I =新的意图(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
i.putExtra(MediaStore.EXTRA_OUTPUT,
MyFileContentProviders.CONTENT_URI);
的getParent()。startActivityForResult(ⅰ,
CAMERA_REQUEST);
} 其他 {
Toast.makeText(的getParent(),
相机是不可用,
Toast.LENGTH_LONG).show();
}
}否则如果(项目[项目] ==画廊){
尝试 {
意向意图=新的意图();
intent.setType(图像/ *);
intent.setAction(Intent.ACTION_GET_CONTENT);
的getParent()。startActivityForResult(
Intent.createChooser(意向,
选择图片),PICK_IMAGE);
}赶上(例外五){
Toast.makeText(的getParent(),e.getMessage(),
Toast.LENGTH_LONG).show();
Log.e(e.getClass()的getName(),e.getMessage(),E。);
}
}
}
});
AlertDialog警报= builder.create();
alert.show();
}
});
在活动结果:
公共无效onActivityResult(INT申请code,INT结果code,意图数据){
super.onActivityResult(要求code,因此code,数据);
捆绑亿= data.getExtras();
开关(要求code){
案例PICK_IMAGE:
如果(结果code == Activity.RESULT_OK){
乌里selectedImageUri = data.getData();
字符串的文件路径= NULL;
尝试 {
// OI文件管理器
串filemanagerstring = selectedImageUri.getPath();
//媒体库
字符串selectedImagePath = getPath(selectedImageUri);
// logo.setImageURI(selectedImageUri);
如果(selectedImagePath!= NULL){
文件路径= selectedImagePath;
}否则,如果(filemanagerstring!= NULL){
文件路径= filemanagerstring;
} 其他 {
Toast.makeText(getApplicationContext(),未知道路,
Toast.LENGTH_LONG).show();
Log.e(位图,未知道路);
}
如果(文件路径!= NULL){
// /upload.setText(filepath);
德codeFILE(文件路径);
} 其他 {
//文件路径= NULL;
位= NULL;
}
}赶上(例外五){
Toast.makeText(getApplicationContext(),内部错误,
Toast.LENGTH_LONG).show();
Log.e(e.getClass()的getName(),e.getMessage(),E。);
}
}
打破;
案例CAMERA_REQUEST:
如果(结果code == Activity.RESULT_OK){
文件输出=新的文件(getFilesDir(),newImage.jpg);
如果(!out.exists()){
Toast.makeText(getBaseContext(),
错误而捕获图像,Toast.LENGTH_LONG)
。显示();
返回;
}
。字符串的文件路径= out.getAbsolutePath()的toString();
德codeFILE(文件路径);
}
打破;
默认:
}
完成异常显示在下面的图片
如果你需要更多的东西,请告诉我。
在此先感谢
等待答案
TakePicture.setOnClickListener(新OnClickListener(){
@覆盖
公共无效的onClick(视图v){
意向意图=新的意图(MediaStore.ACTION_IMAGE_CAPTURE);
档案文件=新的文件(Environment.getExternalStorageDirectory()getPath(),MyPic - + System.currentTimeMillis的()+.JPG。);
SelectedImage = Uri.fromFile(文件);
intent.putExtra(MediaStore.EXTRA_OUTPUT,SelectedImage);
startActivityForResult(意向,CAMERA_PIC_REQUEST);
}
});
SelectfromGallery.setOnClickListener(新OnClickListener(){
@覆盖
公共无效的onClick(视图v){
意向意图=新的意图();
intent.setType(图像/ *);
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(意向,选择图片),RESULT_LOAD_IMAGE);
}
});
@覆盖
保护无效onActivityResult(INT申请code,INT结果code,意图数据){
super.onActivityResult(要求code,因此code,数据);
如果(要求code == RESULT_LOAD_IMAGE和放大器;&安培;结果code == RESULT_OK和放大器;&安培;!NULL =数据){
SelectedImage = data.getData();
字符串文件路径= NULL;
尝试 {
// IO文件管理器
串filemanagerstring = SelectedImage.getPath();
//媒体库
字符串selectedImagePath = getPath(SelectedImage);
如果(selectedImagePath!= NULL){
文件路径= selectedImagePath;
}否则,如果(filemanagerstring!= NULL){
文件路径= filemanagerstring;
} 其他 {
Toast.makeText(getApplicationContext(),未知道路,
Toast.LENGTH_LONG).show();
Log.e(位图,未知道路);
}
如果(文件路径!= NULL){
德codeFILE(文件路径);
} 其他 {
位= NULL;
}
FROM_GALLERY = TRUE;
}赶上(例外五){
Log.e(uploadError时,e.getLocalizedMessage());
}
}
否则,如果(要求code == CAMERA_PIC_REQUEST和放大器;&安培;结果code == RESULT_OK){
/ * // SelectedImage = data.getData();
。位图的缩略图=(位图)data.getExtras()获得(数据);
ProfilePic.setImageBitmap(缩略图); * /
字符串文件路径= NULL;
尝试 {
// OI文件管理器
串filemanagerstring = SelectedImage.getPath();
//媒体库
字符串selectedImagePath = getPath(SelectedImage);
如果(selectedImagePath!= NULL){
文件路径= selectedImagePath;
}否则,如果(filemanagerstring!= NULL){
文件路径= filemanagerstring;
} 其他 {
Toast.makeText(getApplicationContext(),未知道路,
Toast.LENGTH_LONG).show();
Log.e(位图,未知道路);
}
如果(文件路径!= NULL){
德codeFILE(文件路径);
FROM_GALLERY = FALSE;
} 其他 {
位= NULL;
}
}赶上(例外五){
Log.e(错误,e.getLocalizedMessage());
}
}
否则,如果(结果code == Activity.RESULT_CANCELED)
{
Log.e(状态:,选择画面取消);
}
}
公共无效去codeFILE(字符串文件路径){
//德code图像尺寸
BitmapFactory.Options O =新BitmapFactory.Options();
o.inJustDe codeBounds = TRUE;
BitmapFactory.de codeFILE(文件路径,O);
//我们希望新的大小扩展到
最终诠释REQUIRED_SIZE = 1024;
//找到正确的比例值。它应该是2的幂。
INT width_tmp = o.outWidth,height_tmp = o.outHeight;
int标= 1;
而(真){
如果(width_tmp< REQUIRED_SIZE和放大器;&安培; height_tmp< REQUIRED_SIZE)
打破;
width_tmp / = 2;
height_tmp / = 2;
规模* = 2;
}
//德code与inSampleSize
BitmapFactory.Options O2 =新BitmapFactory.Options();
o2.inSampleSize =规模;
位= BitmapFactory.de codeFILE(文件路径,O2);
ProfilePic.setImageBitmap(位);
}
在这里你去。我完全code来实现这一目标。我不能做彻底的测试,我有有限的设备。因此,不能肯定地说,这将在所有的设备上。如果你发现你的问题,或者如果code ++工程,然后让我知道一个解决方案。谢谢。快乐编码。
I am trying to use camera class in my app. I just want to click a picture and set on the imageview and then posting it on some url. Posting on url working fine but sometime problem occurs while clicking any picture and resuming back to same activity from where I am navigating to camera app. It works fine on HTC wildfire (2.2 version) but sometime gives exception (failure chance 1/25) but when i test it on Sony xperia miro or samsung tab (4.0 version) it gives same exception many times (failure chance 20/25). I am not getting where the problem exists because sometimes app runs smoothly without any exception but with 4.0 or above version it force closes many times but sometime works fine on it.
Exception is : java.lang.RuntimeException: Unable to resume activity {fable.eventippo/fable.eventippo.EventsIppoPaymentActivity}:java.lang.RuntimeException: Failure delivering result ResultInfo{who=tabHome, request=1, result=-1, data=Intent{ dat=content://media/external/images/media/17271 }} to activity {fable.eventippo/fable.eventippo.EventsIppoPaymentActivity}: java.lang.ClassCastException: fable.eventippo.Home cannot be cast to fable.eventippo.AddEvent
Complete code is given here.
Button Onclick.
browse = (Button) findViewById(R.id.browse);
browse.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final CharSequence[] items = { "Camera", "Gallery" };
AlertDialog.Builder builder = new AlertDialog.Builder(
getParent());
builder.setTitle("Browse From");
builder.setItems(items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
if (items[item] == "Camera") {
PackageManager pm = getPackageManager();
if (pm.hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
Intent i = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
i.putExtra(MediaStore.EXTRA_OUTPUT,
MyFileContentProviders.CONTENT_URI);
getParent().startActivityForResult(i,
CAMERA_REQUEST);
} else {
Toast.makeText(getParent(),
"Camera is not available",
Toast.LENGTH_LONG).show();
}
} else if (items[item] == "Gallery") {
try {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
getParent().startActivityForResult(
Intent.createChooser(intent,
"Select Picture"), PICK_IMAGE);
} catch (Exception e) {
Toast.makeText(getParent(), e.getMessage(),
Toast.LENGTH_LONG).show();
Log.e(e.getClass().getName(), e.getMessage(), e);
}
}
}
});
AlertDialog alert = builder.create();
alert.show();
}
});
On Activity Result:
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Bundle bn = data.getExtras();
switch (requestCode) {
case PICK_IMAGE:
if (resultCode == Activity.RESULT_OK) {
Uri selectedImageUri = data.getData();
String filepath = null;
try {
// OI FILE Manager
String filemanagerstring = selectedImageUri.getPath();
// MEDIA GALLERY
String selectedImagePath = getPath(selectedImageUri);
// logo.setImageURI(selectedImageUri);
if (selectedImagePath != null) {
filepath = selectedImagePath;
} else if (filemanagerstring != null) {
filepath = filemanagerstring;
} else {
Toast.makeText(getApplicationContext(), "Unknown path",
Toast.LENGTH_LONG).show();
Log.e("Bitmap", "Unknown path");
}
if (filepath != null) {
// /upload.setText(filepath);
decodeFile(filepath);
} else {
// filePath = null;
bitmap = null;
}
} catch (Exception e) {
Toast.makeText(getApplicationContext(), "Internal error",
Toast.LENGTH_LONG).show();
Log.e(e.getClass().getName(), e.getMessage(), e);
}
}
break;
case CAMERA_REQUEST:
if (resultCode == Activity.RESULT_OK) {
File out = new File(getFilesDir(), "newImage.jpg");
if (!out.exists()) {
Toast.makeText(getBaseContext(),
"Error while capturing image", Toast.LENGTH_LONG)
.show();
return;
}
String filepath = out.getAbsolutePath().toString();
decodeFile(filepath);
}
break;
default:
}
Complete Exception is shown on following image
If you need anything more please tell me.
Thanks in Advance
Waiting for answer
TakePicture.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File file = new File(Environment.getExternalStorageDirectory().getPath(), "MyPic-"+ System.currentTimeMillis() + ".jpg");
SelectedImage = Uri.fromFile(file);
intent.putExtra(MediaStore.EXTRA_OUTPUT, SelectedImage);
startActivityForResult(intent,CAMERA_PIC_REQUEST);
}
});
SelectfromGallery.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,"Select Picture"), RESULT_LOAD_IMAGE);
}
});
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
SelectedImage = data.getData();
String filePath = null;
try {
// IO FILE Manager
String filemanagerstring = SelectedImage.getPath();
// MEDIA GALLERY
String selectedImagePath = getPath(SelectedImage);
if (selectedImagePath != null) {
filePath = selectedImagePath;
} else if (filemanagerstring != null) {
filePath = filemanagerstring;
} else {
Toast.makeText(getApplicationContext(), "Unknown path",
Toast.LENGTH_LONG).show();
Log.e("Bitmap", "Unknown path");
}
if (filePath != null) {
decodeFile(filePath);
} else {
bitmap = null;
}
FROM_GALLERY = true;
} catch (Exception e) {
Log.e("Uploaderror", e.getLocalizedMessage());
}
}
else if(requestCode==CAMERA_PIC_REQUEST && resultCode == RESULT_OK){
/*//SelectedImage = data.getData();
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
ProfilePic.setImageBitmap(thumbnail);*/
String filePath = null;
try {
// OI FILE Manager
String filemanagerstring = SelectedImage.getPath();
// MEDIA GALLERY
String selectedImagePath = getPath(SelectedImage);
if (selectedImagePath != null) {
filePath = selectedImagePath;
} else if (filemanagerstring != null) {
filePath = filemanagerstring;
} else {
Toast.makeText(getApplicationContext(), "Unknown path",
Toast.LENGTH_LONG).show();
Log.e("Bitmap", "Unknown path");
}
if (filePath != null) {
decodeFile(filePath);
FROM_GALLERY = false;
} else {
bitmap = null;
}
} catch (Exception e) {
Log.e("error:", e.getLocalizedMessage());
}
}
else if (resultCode == Activity.RESULT_CANCELED)
{
Log.e("STATUS:", "Selecting picture cancelled");
}
}
public void decodeFile(String filePath) {
// Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeFile(filePath, o);
// The new size we want to scale to
final int REQUIRED_SIZE = 1024;
// Find the correct scale value. It should be the power of 2.
int width_tmp = o.outWidth, height_tmp = o.outHeight;
int scale = 1;
while (true) {
if (width_tmp < REQUIRED_SIZE && height_tmp < REQUIRED_SIZE)
break;
width_tmp /= 2;
height_tmp /= 2;
scale *= 2;
}
// Decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
bitmap = BitmapFactory.decodeFile(filePath, o2);
ProfilePic.setImageBitmap(bitmap);
}
here you go. My complete code to achieve that objective. I couldn't do thorough testing as i had limited devices. So, can't say for sure that this would work on all devices. if you find out a solution for your issue or if this code works then let me know. Thanks. Happy coding.
这篇关于java.lang.RuntimeException的:无法恢复活动{packagename.classname}:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!