onActivityResult不起作用

时间:2013-05-31 12:36:46

标签: android camera gallery

目前正在处理从gallery / cammera获取图像并将其设置在imageview上的任务,我正在使用tabgroup活动。这个onActivityResult没有调用。请检查我的代码,请给我一些解决方案。< / p>

public class Create_Event extends BaseActivity{
    ImageView prof;
    String imagepath;
    Button next;
    private static int RESULT_LOAD_IMAGE = 1;
    @SuppressLint("HandlerLeak")
    public void onCreate(Bundle savedInstanceState) {
        setLayoutId(this, R.layout.createevent);
        super.onCreate(savedInstanceState);
        mHandler = new Handler() 
        {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            switch (msg.what) {
            case Constants.MSG_CHOOSE_PHOTO: {
                   Intent i = new Intent(
                            Intent.ACTION_PICK,
                            android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                  //  startActivityForResult(i,Constants.MSG_CHOOSE_PHOTO);
                       System.out.println("fghdfgldgdjlgjdjgldfjgdfgdfjgl;f");
                           startActivityForResult(i,Constants.MSG_CHOOSE_PHOTO);
            }
                break;
        }
        }
                };

         initview();


    }
    public void initview() {
        prof=(ImageView)findViewById(R.id.eventimg);
        prof.setOnClickListener(this);
        next =(Button)findViewById(R.id.button1);
        next.setOnClickListener(this);
          if(Constants.getPicture_path().toString().length()>0)
           {
            prof.setImageBitmap(BitmapFactory.decodeFile(Constants.getPicture_path().toString()));
           }


          registerForContextMenu(prof);
    }

      public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) { 
            super.onCreateContextMenu(menu, v, menuInfo); 
            menu.setHeaderTitle("Choose Image from"); 
             menu.add(0, v.getId(), 0, "Gallery"); 
             menu.add(0, v.getId(), 0, "camera"); 
           }   
           @Override 
           public boolean onContextItemSelected(MenuItem item)
       { 
            if(item.getTitle()=="Gallery")
            {
                function1(item.getItemId());
            } 
             else if(item.getTitle()=="camera")
            {
                function2(item.getItemId());
             }  
            else
            {
            return false;
            } 
             return true; 
       }
       private void function1(int itemId)
       {
           mHandler.sendEmptyMessage(Constants.MSG_CHOOSE_PHOTO);




       }
       private void function2(int itemId)
       {
            Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
            intent.putExtra(MediaStore.EXTRA_OUTPUT, "/mnt/sdcard/profile_image.jpg");
            imagepath="/mnt/sdcard/montacts_profile_image.jpg";
            startActivityForResult(intent, 1);
       } 








        @Override
        public void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
              System.out.println("eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee;f");

            if (requestCode == Constants.MSG_CHOOSE_PHOTO && resultCode == RESULT_OK && null != data) {
                Uri selectedImage = data.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 picturePath = cursor.getString(columnIndex);
                cursor.close();

                imagepath=picturePath;

                Constants.setPicture_path(picturePath);

                     //  Bitmap b = BitmapFactory.decodeByteArray(imageAsBytes, 0, imageAsBytes.length)
                        //profileImage.setImageBitmap(Bitmap.createScaledBitmap(b, 120, 120, false));
                prof.setImageBitmap(BitmapFactory.decodeFile(picturePath));

                    }
            else if(requestCode == 1){

                if(data != null)
                {
                    Bitmap photo = (Bitmap) data.getExtras().get("data");
                    photo = photo.createScaledBitmap(photo, 80, 80, false);
                    prof.setImageBitmap(photo);
                }
                else
                {

                }

            }

    }


   public void onClick(View view) {
        int viewId = view.getId();
        switch (viewId) {
        case R.id.eventimg:
        {
            //Intent i=new Intent(this,New_imgsel.class);
            //startActivity(i);

        }
            break;
        case R.id.button1:
        {


        }
            break;
        }
   }



}

2 个答案:

答案 0 :(得分:0)

 if(item.getTitle()=="Gallery")
 {
      function1(item.getItemId());
 } 
 else if(item.getTitle()=="camera")
 {
      function2(item.getItemId());
 }  

函数1和函数2从不被调用,因为你以错误的方式比较字符串。您必须使用equalseqaulsIgnoreCase

 if(item.getTitle().equalsIgnoreCase("Gallery"))
 {
      function1(item.getItemId());
 } 
 else if(item.getTitle().equalsIgnoreCase("camera"))
 {
      function2(item.getItemId());
 }  

使用==比较字符串的地址

答案 1 :(得分:0)

从父活动调用startActivityForResult并在那里捕获结果。