【问题标题】:onActivityResult is called before ACTION_PICK is finished在 ACTION_PICK 完成之前调用 onActivityResult
【发布时间】:2012-05-30 16:59:16
【问题描述】:

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

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,但这不起作用。或者我应该更改“ACTION_PICK”活动的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(参见上面的“这条线”)

应用程序停止,但当我重新启动它时,我看到它已按照应有的方式选择照片

【问题讨论】:

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


    【解决方案1】:

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

    switch() {
       case 1:
          break;
    
       case 2:
          break;
    }
    

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

    【讨论】:

    • 好吧,我真是太笨了...这解决了问题..谢谢伙计:p
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-15
    • 2018-11-01
    • 1970-01-01
    • 2011-05-14
    相关资源
    最近更新 更多