1

目前正在处理从画廊/相机获取图像并将其设置在图像视图上的任务,并且我正在使用选项卡组活动。在此 onActivityResult 没有调用。请检查我的代码,请给我一些解决方案。

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



}
4

2 回答 2

0

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

于 2013-05-31T22:09:38.433 回答
0
 if(item.getTitle()=="Gallery")
 {
      function1(item.getItemId());
 } 
 else if(item.getTitle()=="camera")
 {
      function2(item.getItemId());
 }  

function1 和 function2 永远不会被调用,因为您以错误的方式比较字符串。您必须使用equalseqaulsIgnoreCase

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

==您比较字符串的地址

于 2013-05-31T12:51:38.353 回答