在 Android 中添加图像原因:无法恢复活动:未能交付结果 ResultInfowho=null, request=100, result=-1, data=null
Posted
技术标签:
【中文标题】在 Android 中添加图像原因:无法恢复活动:未能交付结果 ResultInfowho=null, request=100, result=-1, data=null【英文标题】:Adding Image in Android cause: Unable to resume activity : Failure delivering result ResultInfowho=null, request=100, result=-1, data=null在 Android 中添加图像原因:无法恢复活动:未能交付结果 ResultInfowho=null, request=100, result=-1, data=null 【发布时间】:2015-02-06 03:16:40 【问题描述】:请帮助我使用这款 android 相机。
我得到的错误如下:
Unable to resume activity ..,CreatePropertyActivity5: Failure delivering result ResultInfowho=null, request=100, result=-1, data=null to activity..: java.lang.NullPointer
它还给出了这个奇怪的警告,因为我压缩了图像:
W/OpenGLRenderer(10242): Bitmap too large to be uploaded into a texture (3264x2448, max=2048x2048)
我的应用程序允许用户从图库中选择一张照片或使用相机拍摄一张。 从代码中可以看出,用户添加的照片数量没有限制。
在主页 (createPropertyActivity*5*) 中,用户触摸拍照按钮,她选择相机或画廊,然后她选择(或拍摄)一张照片,然后应用程序转到一个页面 (CreatePropertyActivity*51* ),她可以在其中取消或为图像写描述。 当她触摸“发送照片按钮”时,她返回主页,照片被发送到服务器,并且图像视图被添加到主页中先前添加的图像的底部,以便用户可以看到她的所有图像已选择。
有时(我不知道何时会发生这种情况,有时是添加第四张图片时,有时是添加第一张图片时!)它给出了错误! (尤其是当相机处于高分辨率时)。
我压缩照片和所有内容。我究竟做错了什么? 和文件名有关系吗?压缩?
我的代码有什么问题?我不知道!
这是我的代码:
public class CreatePropertyActivity5 extends ActionBarActivity
protected static final int SELECT_PICTURE = 1;
private static final int ACTIVITY_REQUEST_CODE_IMAGE = 100;
private static final int IMAGE_DESCRIPTION = 200;
LinearLayout ll;
private List<File> cameraImageFiles;
private JSONRequestForCreatePropertyListing propertyListing;
ImageView imageView;
Bitmap selectedImageBitmap;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create_property_5);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
propertyListing = (JSONRequestForCreatePropertyListing) getIntent().getSerializableExtra("JSONRequestForCreatePropertyListing");
CreatePropertListingAsync cplp = new CreatePropertListingAsync(this, propertyListing);
cplp.execute();
@Override
public boolean onCreateOptionsMenu(Menu menu)
getMenuInflater().inflate(R.menu.create_property_activity5, menu);
return true;
@Override
public boolean onOptionsItemSelected(MenuItem item)
int id = item.getItemId();
if (id == R.id.action_settings)
return true;
return super.onOptionsItemSelected(item);
public void onClickTakePicture(View v) throws IOException
// Camera.
final List<Intent> cameraIntents = new ArrayList<Intent>();
final Intent captureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
final PackageManager packageManager = getPackageManager();
final List<ResolveInfo> listCam = packageManager.queryIntentActivities(captureIntent, 0);
cameraImageFiles = new ArrayList<File>();
int i=0;
for(ResolveInfo res : listCam)
final String packageName = res.activityInfo.packageName;
final Intent intent = new Intent(captureIntent);
intent.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
intent.setPackage(packageName);
intent.putExtra(MediaStore.MEDIA_IGNORE_FILENAME, ".nomedia");
//** below 4 lines put the uri of the camera taken picture to the EXTRA_OUTPUT
File cameraImageOutputFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "myFileName");
cameraImageFiles.add(cameraImageOutputFile);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(cameraImageFiles.get(i)));
i++;
cameraIntents.add(intent);
// Filesystem.
final Intent galleryIntent = new Intent();
galleryIntent.setType("image/*");
galleryIntent.setAction(Intent.ACTION_GET_CONTENT);
// Chooser of filesystem options.
final Intent chooserIntent = Intent.createChooser(galleryIntent, "add new");
// Add the camera options.
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, cameraIntents.toArray(new Parcelable[]));
startActivityForResult(chooserIntent, ACTIVITY_REQUEST_CODE_IMAGE);
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent)
ll = (LinearLayout) findViewById(R.id.llCreatePropertyImages);
switch(requestCode)
// For sending photos to server. We come here from activity51
case IMAGE_DESCRIPTION:
if(resultCode == RESULT_OK)
//add the image to the activity5 page.
imageView = new ImageView(this);
imageView.setPadding(0, 10, 0, 0);
ll.setVisibility(View.VISIBLE);
imageView.setImageBitmap(selectedImageBitmap);
ll.addView(imageView);
String s = imageReturnedIntent.getStringExtra("key");
//user entered description is in "key"
imageView.setTag(s);
Bitmap bitmap1 = ((BitmapDrawable)imageView.getDrawable()).getBitmap();
ByteArrayOutputStream stream=new ByteArrayOutputStream();
bitmap1.compress(Bitmap.CompressFormat.PNG, 90, stream);
byte[] image=stream.toByteArray();
String img_str = Base64.encodeToString(image, 0);
//This part sends the picture to the server
ArrayList<Photos> photos = new ArrayList<Photos>();
photos.add(new Photos(new Ax(img_str)));
int id = Integer.parseInt((String) ((TextView) findViewById(R.id.txt_property_listing_ID)).getText());
int editPass = Integer.parseInt((String) ((TextView) findViewById(R.id.txt_property_listing_password)).getText());
JSONRequestForAddPhoto jr = new JSONRequestForAddPhoto(id, editPass, photos);
new AddPhotoAsync(this, jr).execute();
break;
//For choosing photos from gallery or taking one with camera
case ACTIVITY_REQUEST_CODE_IMAGE:
if(resultCode == RESULT_OK)
Uri uri = null;
if(imageReturnedIntent == null) //since we used EXTRA_OUTPUT for camera, so it will be null
for(int i=0;i<cameraImageFiles.size();i++)
if(cameraImageFiles.get(i).exists())
uri = Uri.fromFile(cameraImageFiles.get(i));
break;
else // from gallery
uri = imageReturnedIntent.getData();
if(uri != null)
try
Bitmap bitmap3 = decodeSampledBitmapFromResource(uri, 500, 500);
selectedImageBitmap = bitmap3;
Intent i= new Intent(this, CreatePropertyActivity51.class);
i.putExtra("photoUri", uri);
startActivityForResult(i,IMAGE_DESCRIPTION);
//*** show activity51
catch (FileNotFoundException e)
// TODO Auto-generated catch block
e.printStackTrace();
catch (Exception e)
// TODO Auto-generated catch block
e.printStackTrace();
public Bitmap decodeSampledBitmapFromResource(Uri uri, int reqWidth, int reqHeight) throws IOException
ContentResolver cr = getContentResolver();
InputStream inStream = cr.openInputStream(uri);
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeStream(inStream, null, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
inStream.close();
inStream = cr.openInputStream(uri);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
Bitmap result = BitmapFactory.decodeStream(inStream, null , options);
inStream.close();
return result;
public int calculateInSampleSize(
BitmapFactory.Options options, int reqWidth, int reqHeight)
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth)
final int halfHeight = height / 2;
final int halfWidth = width / 2;
// Calculate the largest inSampleSize value that is a power of 2 and keeps both
// height and width larger than the requested height and width.
while ((halfHeight / inSampleSize) > reqHeight
&& (halfWidth / inSampleSize) > reqWidth)
inSampleSize *= 2;
return inSampleSize;
第二个活动:
public class CreatePropertyActivity51 extends ActionBarActivity
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create_property_51);
ImageView imageView = (ImageView) findViewById(R.id.img_selected_image);
Uri uri = getIntent().getParcelableExtra("photoUri");
Bitmap bitmap3;
try
bitmap3 = decodeSampledBitmapFromResource(uri, 200, 200);
imageView.setImageBitmap(bitmap3);
catch (IOException e)
// TODO Auto-generated catch block
e.printStackTrace();
@Override
public boolean onCreateOptionsMenu(Menu menu)
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.create_property_activity51, menu);
return true;
@Override
public boolean onOptionsItemSelected(MenuItem item)
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings)
return true;
return super.onOptionsItemSelected(item);
public void sendPhoto(View v)
Intent data = new Intent();
data.putExtra("key", ((EditText)findViewById(R.id.etxt_photo_description)).getText().toString());
setResult(Activity.RESULT_OK, data);
finish();
public void cancelSendPhoto(View v)
Intent data = new Intent();
setResult(Activity.RESULT_CANCELED, data);
finish();
public Bitmap decodeSampledBitmapFromResource(Uri uri, int reqWidth, int reqHeight) throws IOException
ContentResolver cr = getContentResolver();
InputStream inStream = cr.openInputStream(uri);
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeStream(inStream, null, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
inStream.close();
inStream = cr.openInputStream(uri);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
Bitmap result = BitmapFactory.decodeStream(inStream, null , options);
inStream.close();
return result;
public int calculateInSampleSize(
BitmapFactory.Options options, int reqWidth, int reqHeight)
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth)
final int halfHeight = height / 2;
final int halfWidth = width / 2;
// Calculate the largest inSampleSize value that is a power of 2 and keeps both
// height and width larger than the requested height and width.
while ((halfHeight / inSampleSize) > reqHeight
&& (halfWidth / inSampleSize) > reqWidth)
inSampleSize *= 2;
return inSampleSize;
LogCat:
12-08 17:14:50.448: D/dalvikvm(12330): GC_FOR_ALLOC freed 619K, 16% free 19947K/23495K, paused 20ms, total 25ms 12-08 17:14:52.003:
D/dalvikvm(12330):GC_FOR_ALLOC 释放 942K,15% 释放 20023K/23495K, 暂停 20 毫秒,总共 20 毫秒 12-08 17:14:52.003: I/dalvikvm-heap(12330): 将堆(碎片情况)增加到 21.958MB 以分配 2092136 字节 12-08 17:14:52.050:D/dalvikvm(12330):GC_CONCURRENT 释放 0K,14% 释放 22066K/25543K,暂停12ms+4ms,共42ms 12-08 17:14:53.940: D/dalvikvm(12330):GC_FOR_ALLOC 释放 1021K,18% 释放 21045K/25543K, 暂停 19 毫秒,总共 19 毫秒 12-08 17:14:53.948: I/dalvikvm-heap(12330): 将堆(碎片情况)增加到 22.560MB 以分配 1675864 字节 12-08 17:14:53.971: D/dalvikvm(12330): GC_FOR_ALLOC 释放 0K,17% 释放 22681K/27207K,暂停21ms,共21ms 12-08 17:14:53.987: D/dalvikvm(12330):GC_FOR_ALLOC 释放 0K,17% 释放 22681K/27207K, 暂停 19ms,总共 19ms 12-08 17:14:53.995: I/dalvikvm-heap(12330): 将堆(碎片情况)增加到 24.719MB 以分配 2263881 字节 12-08 17:14:54.026: D/dalvikvm(12330): GC_FOR_ALLOC 释放 0K,释放 16% 24892K/29447K,暂停31ms,共31ms 12-08 17:14:54.089: D/dalvikvm(12330):GC_CONCURRENT 释放 0K,16% 释放 24892K/29447K, 暂停 17ms+14ms,总共 62ms 12-08 17:14:55.643: D/dalvikvm(12330): GC_FOR_ALLOC 释放 .unregisterListener : / 监听器计数 = 0->0, ubvf 9budiwrd5ordgfl5BakTrklMrfo$,@,)aa):88 12-08 17:15:01.643: D/dalvikvm(12330):GC_CONCURRENT 释放 18K,22% 释放 32567K/41479K, 暂停 13ms+4ms,总共 40ms 12-08 17:15:01.643: D/dalvikvm(12330): WAIT_FOR_CONCURRENT_GC 阻塞了 26 毫秒 12-08 17:15:01.690: D/dalvikvm(12330):GC_FOR_ALLOC 释放 4657K,33% 释放 27910K/41479K, 暂停 45ms,总共 45ms 12-08 17:15:01.690: I/dalvikvm-heap(12330): 将堆(碎片情况)增加到 29.853MB 以分配 2293502 字节 12-08 17:15:01.729:D/dalvikvm(12330):GC_CONCURRENT 释放 0K,释放 28% 30150K/41479K,暂停4ms+4ms,共39ms 12-08 17:15:01.729: D/dalvikvm(12330):WAIT_FOR_CONCURRENT_GC 阻塞 6ms 12-08 17:15:23.596:D/AndroidRuntime(12330):关闭 VM 12-08 17:15:23.596:W/dalvikvm(12330):threadid = 1:线程退出 未捕获的异常(组=0x41d0e2a0)12-08 17:15:23.635: E/AndroidRuntime(12330):致命异常:主要 12-08 17:15:23.635: E/AndroidRuntime(12330): java.lang.RuntimeException: 无法恢复 活动 com.appName.appName/com.appName.appName.CreatePropertyActivity5: java.lang.RuntimeException:传递结果失败 ResultInfowho=null, request=100, result=-1, data=null 到活动 com.appName.appName/com.appName.appName.CreatePropertyActivity5: java.lang.NullPointerException 12-08 17:15:23.635: E/AndroidRuntime(12330):在 android.app.ActivityThread.performResumeActivity(ActivityThread.java:2636) 12-08 17:15:23.635: E/AndroidRuntime(12330): 在 android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2664) 12-08 17:15:23.635: E/AndroidRuntime(12330): 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2137) 12-08 17:15:23.635: E/AndroidRuntime(12330): 在 android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3573) 12-08 17:15:23.635: E/AndroidRuntime(12330): 在 android.app.ActivityThread.access$800(ActivityThread.java:140) 12-08 17:15:23.635:E/AndroidRuntime(12330):在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1244) 12-08 17:15:23.635: E/AndroidRuntime(12330): 在 android.os.Handler.dispatchMessage(Handler.java:99) 12-08 17:15:23.635:E/AndroidRuntime(12330):在 android.os.Looper.loop(Looper.java:137) 12-08 17:15:23.635: E/AndroidRuntime(12330):在 android.app.ActivityThread.main(ActivityThread.java:4918) 12-08 17:15:23.635:E/AndroidRuntime(12330):在 java.lang.reflect.Method.invokeNative(Native Method) 12-08 17:15:23.635:E/AndroidRuntime(12330):在 java.lang.reflect.Method.invoke(Method.java:511) 12-08 17:15:23.635: E/AndroidRuntime(12330):在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:994) 12-08 17:15:23.635: E/AndroidRuntime(12330): 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761) 12-08 17:15:23.635:E/AndroidRuntime(12330):在 dalvik.system.NativeStart.main(本机方法)12-08 17:15:23.635: E/AndroidRuntime(12330):引起:java.lang.RuntimeException: 传递结果失败 ResultInfowho=null, request=100, result=-1, data=null 到活动 com.appName.appName/com.appName.appName.CreatePropertyActivity5: java.lang.NullPointerException 12-08 17:15:23.635: E/AndroidRuntime(12330):在 android.app.ActivityThread.deliverResults(ActivityThread.java:3202) 12-08 17:15:23.635: E/AndroidRuntime(12330): 在 android.app.ActivityThread.performResumeActivity(ActivityThread.java:2623) 12-08 17:15:23.635: E/AndroidRuntime(12330): ... 13 更多 12-08 17:15:23.635:E/AndroidRuntime(12330):由以下原因引起: java.lang.NullPointerException 12-08 17:15:23.635: E/AndroidRuntime(12330):在 com.appName.appName.CreatePropertyActivity5.onActivityResult(CreatePropertyActivity5.java:167) 12-08 17:15:23.635: E/AndroidRuntime(12330): 在 android.app.Activity.dispatchActivityResult(Activity.java:5369) 12-08 17:15:23.635:E/AndroidRuntime(12330):在 android.app.ActivityThread.deliverResults(ActivityThread.java:3198) 12-08 17:15:23.635: E/AndroidRuntime(12330): ... 14 更多
【问题讨论】:
请发布带有完整堆栈跟踪的 logcat。 您在第 167 行的CreatePropertyActivity5
有 NullPointerException
。那是哪一行?
cameraImageFiles in for(int i=0;iCreatePropertyActivity5.onCreate()
并在调用 onActivityResult()
之前查看是否正在重新创建此活动。有可能Android正在杀死您的进程,而用户正在使用相机和图库,然后返回到您的应用程序时,您的所有变量都为空。
显然你是对的。每次发生时,都会调用 onCreate。你建议我做什么?即使我将字段设为静态以避免获得 NPE,这对我也没有好处。因为 onCreate 会再次附加布局,之前添加的所有图像都将消失。
【参考方案1】:
这正是我所期望的。 Android 在相机或图库运行时杀死您的进程,因为它需要资源。当用户返回您的应用程序时,Android 已启动一个新进程并正在重新创建您的活动。您需要做的是覆盖 onSaveInstanceState()
和 onRestoreInstanceState()
并保存和恢复用户在使用相机或图库应用时需要保留的任何内容。
【讨论】:
哇,谢谢@David【参考方案2】:W/OpenGLRenderer(10242):位图太大,无法上传到纹理中(3264x2448,max=2048x2048)这个问题的答案是:
您必须减小位图的大小,如下所示
Bitmap originalImage = BitmapFactory.decodeFile(mCurrentPhotoPath);
int width = originalImage.getWidth()*2/4;
int height = originalImage.getHeight()*2/4;
pic.setImageBitmap(Bitmap.createScaledBitmap(originalImage, width, height, true));
那么它会为你工作
【讨论】:
以上是关于在 Android 中添加图像原因:无法恢复活动:未能交付结果 ResultInfowho=null, request=100, result=-1, data=null的主要内容,如果未能解决你的问题,请参考以下文章