onactivityresult在android中使用Gallery时没有被调用

Jig*_*iya 0 android android-intent

我正在使用此代码在我的Android应用程序中打开一个库.

Intent intent = new Intent(Intent.ACTION_PICK, Media.EXTERNAL_CONTENT_URI);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivityForResult(intent, GALLERY);
Run Code Online (Sandbox Code Playgroud)

它打开画廊,但当我从那里选择任何图像时,它也会关闭,但不会被称为onActivityResult方法.

方法在这里....

protected void onActivityResult(int requestCode, int resultCode, Intent data) {     


    Log.v(TAG, "requestCode = "+requestCode+" resultCode = "+resultCode);
    Log.v(TAG, "inside onActivityResult");

    if (requestCode == GALLERY || requestCode == CAMERA) {
        if (resultCode == RESULT_OK) 
        {
            Log.v(TAG, "inside onActivityResult OK ");
            Log.v("log_tag", "onactivity result: " + requestCode);
            if(requestCode == CAMERA)
            {
                Log.v(TAG, "inside onActivityResult Camera");
                 file = getTempFile(this.getParent());  
                 try {  
                    m_bmOCRBitmap = Media.getBitmap(getContentResolver(), Uri.fromFile(file) );

                    FileOutputStream out = new FileOutputStream(file);

                    int width = m_bmOCRBitmap.getWidth();
                    int height = m_bmOCRBitmap.getHeight();
                    if(width>height)
                    {
                        calwidth = (int)((width * 250)/height);
                        calheight = 250;
                    }
                    else
                    {
                        calheight = (int)((height * 200)/width);
                        calwidth = 200;
                    }

                    Log.v(TAG, "calwidth = "+calwidth+" calheight = "+calheight);
                    bitmap = Bitmap.createScaledBitmap(m_bmOCRBitmap, calwidth, calheight, true);                                                                   

                    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
                    productImage.setImageBitmap(bitmap);

                    } catch (FileNotFoundException e) {  
                      e.printStackTrace();  
                    } catch (IOException e) {  
                      e.printStackTrace();  
                    }  
            }
            else if(requestCode == GALLERY)
            {
                Log.v(TAG, "inside onActivityResult GALARY ");
                String selectedImagePath = getPath(data.getData());
                file = new File(selectedImagePath);                                     
                m_bmOCRBitmap = BitmapFactory.decodeFile(selectedImagePath);
                Log.v("log_tag", "selectedImagePath:" + selectedImagePath + ":" );
                Log.v("log", "first image : "+m_bmOCRBitmap);
                try {
                    FileOutputStream out = new FileOutputStream(file);
                    int width = m_bmOCRBitmap.getWidth();
                    int height = m_bmOCRBitmap.getHeight();
                    if(width>height)
                    {
                        calwidth = (int)((width * 250)/height);
                        calheight = 250;
                    }
                    else
                    {
                        calheight = (int)((height * 200)/width);
                        calwidth = 200;
                    }
                    bitmap = Bitmap.createScaledBitmap(m_bmOCRBitmap, calwidth, calheight, true);                                                                   

                    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
                    productImage.setImageBitmap(bitmap);

                } catch (Exception e) {
                    Log.v("log_tag", "Exception: " + e.toString());
                }
            }                           
        }
    }       
    super.onActivityResult(requestCode, resultCode, data);
}
Run Code Online (Sandbox Code Playgroud)

调用Intent时的警告是

01-04 12:21:06.434: D/PhoneWindow(367): couldn't save which view has focus because the focused view com.android.internal.policy.impl.PhoneWindow$DecorView@4054fb60 has no id.
Run Code Online (Sandbox Code Playgroud)

请注意,我已将此活动作为Tabgoup活动的子活动开始.

请建议我.谢谢.

Arc*_*pgc 5

没有被onActivityResult()触发的可能性很多.

  • 如果您的请求代码<0
  • 如果您已android:launchMode="singleInstance"在Manifest.xml中设置
  • 如果你正在使用片段,那么你必须调用Fragment startActivityOnResult而不是Activity