onactivityresult not getting called while using Ga

2019-09-15 05:39发布

I am opening a gallery in my Android Application using this code.

Intent intent = new Intent(Intent.ACTION_PICK, Media.EXTERNAL_CONTENT_URI);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivityForResult(intent, GALLERY);

It opens gallery but when I am selecting any Image from there its closing also but not called onActivityResult method.

Method is here....

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);
}

the Warning while called Intent is

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.

please note that I have started this activity as child activity from Tabgoup Activity.

please suggest me. Thanks.

4条回答
淡お忘
2楼-- · 2019-09-15 05:51

I added this Method in Parent class like this

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    ProductImageActivity activity = (ProductImageActivity)getLocalActivityManager().getCurrentActivity();
    try {
        activity.onActivityResult(requestCode, resultCode, data,0);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

and in my current class I started new Intent like this.

Intent intent = new Intent(Intent.ACTION_PICK, Media.EXTERNAL_CONTENT_URI);
getParent().startActivityForResult(intent, GALLERY);
查看更多
家丑人穷心不美
3楼-- · 2019-09-15 05:57

Try this & use Intent Like this For Fetching Image From Gallery

        Intent intent = new Intent(Intent.ACTION_PICK);
        intent.setType("image/*");
        startActivityForResult(intent, GALLERY);  

Or

           Intent intent = new Intent(Intent.ACTION_PICK,
                    android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        startActivityForResult(intent, GALLERY);
查看更多
Evening l夕情丶
4楼-- · 2019-09-15 05:59

There are lot of possibilities for onActivityResult() not triggered.

  • If your request code is < 0
  • If you have android:launchMode="singleInstance" set in the Manifest.xml
  • If you are using fragments then you must call the Fragment's startActivityOnResult instead of Activity's
查看更多
闹够了就滚
5楼-- · 2019-09-15 06:18

Check http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_NEW_TASK for the behavior of Intent.FLAG_ACTIVITY_NEW_TASK. It is used for independent activities.

This flag is generally used by activities that want to present a "launcher" style      behavior: they give the user a list of separate things that can be done, which otherwise run completely independently of the activity launching them.

Removing that line, you will get a call to onActivityResult once a image is selected.

    Intent intent = new Intent(Intent.ACTION_PICK, Media.EXTERNAL_CONTENT_URI);
    startActivityForResult(intent, GALLERY);
查看更多
登录 后发表回答