Android 在选项卡Host onActivityResult中,结果不起作用

Android 在选项卡Host onActivityResult中,结果不起作用,android,android-tabhost,Android,Android Tabhost,我有一个TabHost,在这4种不同的活动中。在第一个选项卡中,我有一个选项,可以选择“图库”中的图像或从相机中拍照。现在,我使用了public void on activityresult(int-requestCode,int-resultcode,Intent-Intent)方法..但是,它没有在TabHost中调用..没有TabHost它工作得很好..如何解决这个问题..这是我的代码..请检查我的整个代码..提前谢谢 tblTreePhoto.setOnClickListener(new

我有一个TabHost,在这4种不同的活动中。在第一个选项卡中,我有一个选项,可以选择“图库”中的图像或从相机中拍照。现在,我使用了public void on activityresult(int-requestCode,int-resultcode,Intent-Intent)方法..但是,它没有在TabHost中调用..没有TabHost它工作得很好..如何解决这个问题..这是我的代码..请检查我的整个代码..提前谢谢

tblTreePhoto.setOnClickListener(new OnClickListener() 
        {

            @Override
            public void onClick(View arg0) 
            {
                final AlertDialog.Builder alertDialog = new AlertDialog.Builder(getParent());
                alertDialog.setTitle("Select Photo");
                alertDialog.setPositiveButton("Take Photo", new DialogInterface.OnClickListener() 
                {

                    @Override
                    public void onClick(DialogInterface arg0, int arg1) 
                    {
                        if(Tree_Image.count<3)
                        {
                            Intent i1 = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                            File out = Environment.getExternalStorageDirectory();
                            out = new File(out, "newImage.jpg");
                            i1.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(out));
                            getParent().startActivityForResult(i1, 10); 
                        }
                        else
                        {
                            Toast.makeText(getApplicationContext(), "You can not add more than 2 photos" , 1).show();
                        }
                    }

                });

                alertDialog.setNeutralButton("Gallery", new DialogInterface.OnClickListener() 
                {

                    @Override
                    public void onClick(DialogInterface dialog, int which) 
                    {   
                        if(Tree_Image.count<3)
                        {
                            Intent intent = new Intent();
                            intent.setType("image/*");
                            intent.setAction(Intent.ACTION_GET_CONTENT);
                            getParent().startActivityForResult(Intent.createChooser(intent,"Select Picture"),1);
                        }   
                        else
                        {
                            Toast.makeText(getApplicationContext(), "You can not add more than 2 photos" , 1).show();

                        }
                    }
                });
                alertDialog.setNegativeButton("View Selected Photos", new DialogInterface.OnClickListener() 
                {

                    @Override
                    public void onClick(DialogInterface arg0, int arg1) 
                    {
                        Intent i = new Intent(getParent(),Show_Selected_Images.class);
                        //i.putExtra("cqname", client_name.getText().toString());
                        startActivity(i);
                    }

                });

                alertDialog.show();

            }
        });

您正在调用
getParent()


尝试删除
getParent()

其他方法是这样做的

  • 在按钮上单击(您要打开它)打开另一个活动
  • 在另一个活动的onCreate()上打开该意图
  • onActivityResult设置上一个活动的静态数据并调用finish()
  • 这样,没有人知道您打开了这样一个活动,您将获得您的数据

  • :)

    在哪里设置onClickListener和onActivityResult?在TabHost或Activity中?为什么使用getParent()方法?改为使用当前活动,其中定义了onActivityResult()方法。我在链接中显示了这一点。但仍然不工作我删除了getParent()但它仍然不工作实际上onActivityResult方法没有被调用。谢谢您的回复。它正在工作…但打开库比正常时间要花一些时间
     protected void onActivityResult(int requestCode, int resultcode, Intent intent)
         {
             int orientation =0;
             Log.d("result ","code uis"+resultcode);
    
             super.onActivityResult(requestCode, resultcode, intent);
    
             if (requestCode == 1)
             {
                 if (intent != null && resultcode == RESULT_OK)
                 {             
    
                     Uri selectedImage = intent.getData();
    
                       String[] filePathColumn = {MediaStore.Images.Media.DATA};
                       Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
                       cursor.moveToFirst();
                       int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                       String filePath = cursor.getString(columnIndex);
                       Log.v("log","filePath is : "+filePath);
    
                       cursor.close();
                       try 
                       {
                            ExifInterface exif = new ExifInterface(filePath);
                             orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1);
                            //Toast.makeText(getApplicationContext(), ""+orientation, 1).show();
                            Log.v("log", "ort is "+orientation);
    
                       } 
                       catch (IOException e)
                       {
                           e.printStackTrace();
                       }
    
                       if(bmp != null && !bmp.isRecycled())
                       {
                           bmp = null;               
                       }
    
                       File f = new File(filePath);
                       bmp = decodeFile(f,400,400);
    
                       if (orientation==6)
                       {
                            Matrix matrix = new Matrix();
                            matrix.postRotate(90);
                            bmp = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), matrix, true);
                       }
                       else if (orientation==8)
                       {
                            Matrix matrix = new Matrix();
                            matrix.postRotate(270);
                            bmp = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), matrix, true);
                       }
    
                       else if (orientation==3)
                       {
                            Matrix matrix = new Matrix();
                            matrix.postRotate(180);
                            bmp = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), matrix, true);
                       }
    
                       Log.v("log", "before width is "+bmp.getWidth() + " and height is "+bmp.getHeight());
    
                       bt=Bitmap.createScaledBitmap(bmp,320, 300, false);
                      // Bitmap bt_thumb = Bitmap.createScaledBitmap(bmp, 70, 70, false);
    
    
    
    
                       Intent i = new Intent(Tree_Assessmnt_Tree.this,Tree_Image.class);
                       i.putExtra("userid", uid);
                      // TabGroupActivity parentActivity = (TabGroupActivity) getParent();
                       startActivity(i);
    
                 }
                 else
                 {
                     Log.v("log", "Photopicker canceled");           
                 }
             }
             else if (requestCode == 10)
             {
    
               File out = new File(Environment.getExternalStorageDirectory(), "newImage.jpg");
    
                 if(!out.exists())
                 {
                     Log.v("log", "file not found");
                     Toast.makeText(getBaseContext(),"Error while capturing image", Toast.LENGTH_LONG).show();
    
                    return;
    
                }
    
                 Log.v("log", "file "+out.getAbsolutePath());
                 File f = new File(out.getAbsolutePath());
    
                 try
                 {
    
                    ExifInterface exif = new ExifInterface(out.getAbsolutePath());
                     orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1);
                    //Toast.makeText(getApplicationContext(), ""+orientation, 1).show();
                    Log.v("log", "ort is "+orientation);
    
               } 
                 catch (IOException e)
                 {
                     e.printStackTrace();
                 }
    
                 Bitmap photo =decodeFile(f,400,400);
                 if (orientation==6)
                 {
                        Matrix matrix = new Matrix();
                        matrix.postRotate(90);
                        photo = Bitmap.createBitmap(photo, 0, 0, photo.getWidth(), photo.getHeight(), matrix, true);
                 }
                 else if (orientation==8)
                 {
                        Matrix matrix = new Matrix();
                        matrix.postRotate(270);
                        photo = Bitmap.createBitmap(photo, 0, 0, photo.getWidth(), photo.getHeight(), matrix, true);
                 }
    
                 else if (orientation==3)
                 {
                        Matrix matrix = new Matrix();
                        matrix.postRotate(180);
                        photo = Bitmap.createBitmap(photo, 0, 0, photo.getWidth(), photo.getHeight(), matrix, true);
                 }
    
    
    
               //Bitmap photo = (Bitmap) intent.getExtras().get("data"); 
               //bt=Bitmap.createScaledBitmap(photo, 380, 520, false);
               bt=photo;
              // bt=Bitmap.createScaledBitmap(photo,320, 300, false);
    //         Log.v("log", "before width is "+bt.getWidth() + " and height is "+bt.getHeight());
    
                    /*  gamePic.setBackgroundResource(0);
                      gamePic.setImageBitmap(photo);*/
               //txtPhoto.setText(""+DrawOnImage_Activity.count);
    
    
               Intent i = new Intent(Tree_Assessmnt_Tree.this,Tree_Image.class);
               i.putExtra("userid", uid);
               startActivity(i);
    
             }
    
             else if(requestCode==6)
             {
                  if (intent == null)
                  {
                      Log.v("log", "Null data, but RESULT_OK, from image picker!");
                           Toast t = Toast.makeText(this, "No Photo Slected",Toast.LENGTH_SHORT);
                           t.show();
    
                           return;
                  }
             }
         }