在ACTION_PICK完成之前调用onActivityResult

时间:2012-05-30 16:59:16

标签: android android-activity android-gallery android-photos

我在浏览器中选择图片并在我的应用程序中使用它时遇到了一些问题。 我用这个意图:

Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, ACTIVITY_SELECT_PHOTO);

这就把我带到画廊,我可以在那里挑选一张图片。 但是当我选择一个时,会立即调用onActivityResult, 虽然ACTION_PICK还没有完成所有事情。好吧,我猜这就是问题所在。 我已经阅读了我应该尝试将清单中的活动的launchMode更改为singleTop,但这并不起作用。或者我应该更改&{34; ACTION_PICK" -activity的launchMode? 这甚至可能吗?

final static int ACTIVITY_SELECT_PHOTO = 0;
final static int ACTIVITY_CAMERA_PHOTO = 1;

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) { 
    super.onActivityResult(requestCode, resultCode, imageReturnedIntent); 
    Log.d("onActivityresult", "req = " + requestCode + ", result = " + resultCode);
    switch(requestCode) { 
    case ACTIVITY_SELECT_PHOTO:
        if(resultCode == RESULT_OK){  

            Uri selectedImageUri=imageReturnedIntent.getData();
            String actualPath=getRealPathFromURI(selectedImageUri);
            File file=new File(actualPath);
            Bitmap b=decodeFile(file); //this is new bitmap which you can use for your purpose 

            try {
                ByteArrayOutputStream os = new ByteArrayOutputStream();
                b.compress(Bitmap.CompressFormat.PNG, 55, os);
                compressedByteArray = os.toByteArray();
                iv.setImageBitmap(b);
                currentUser.put("picture", compressedByteArray);
                Toast t = Toast.makeText(this, "Uploading picture...", Toast.LENGTH_LONG);
                t.show();
                currentUser.save();
            } catch (ParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    case ACTIVITY_CAMERA_PHOTO:
        if(resultCode == RESULT_OK){

            Bundle extras = imageReturnedIntent.getExtras();

            //THE LINE BELOW THIS ONE GIVES NULLPOINTEREX WHICH IS OBVIOUS BECAUSE IT's ANOTHER //CASE      
            Bitmap bmp = (Bitmap)extras.get("data");
            ByteArrayOutputStream os = new ByteArrayOutputStream();
            bmp.compress(Bitmap.CompressFormat.PNG, 55, os);
            compressedByteArray = os.toByteArray();
            bmp = BitmapFactory.decodeByteArray(compressedByteArray, 0, compressedByteArray.length);
            iv.setImageBitmap(bmp);
            currentUser.put("picture", compressedByteArray);

        }
        try {
            Toast t = Toast.makeText(this, "Uploading picture...", Toast.LENGTH_LONG);
            t.show();
            currentUser.save();
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

ResultCode = -1(所以RESULT_OK) 但它给了我一个错误:

java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=0, result=-1, data=Intent { dat=content://media/external/images/media/173 }} to activity {com.tapazz/com.tapazz.ShowProfileActivity}: java.lang.NullPointerException

nullPointerException来自不同的RequestCode(参见" THIS LINE"上面)

该应用程序停止,但当我重新启动它时,我发现它已经选择了应该执行的照片

1 个答案:

答案 0 :(得分:0)

这可能与此相关,也可能不相关,但在每个案例之后,您的switch()声明不会break。因此,如果它进入ACTIVITY_SELECT_PHOTO情况,它也将处理ACTIVITY_CAMERA_PHOTO

switch() {
   case 1:
      break;

   case 2:
      break;
}

请注意,在每个break;的末尾始终需要case语句。