画廊 OnActivityResult 在 TabActivity 中不起作用

Posted

技术标签:

【中文标题】画廊 OnActivityResult 在 TabActivity 中不起作用【英文标题】:Gallery OnActivityResult is not working in TabActivity 【发布时间】:2012-08-29 04:58:36 【问题描述】:

我的代码有问题,我正在尝试从图库中获取图像并将其显示在我的 xml 布局中 (问题是当我单击按钮时它会转到图库但当我选择图片时它只是转到另一个标签)

1) 创建标签的主类

public class MainUser extends TabActivit 
  .....

TabSpec tabSpec10 = mTabHost.newTabSpec("tab_test1");
        tabSpec10.setIndicator("Report incident ");
        Context ctx10 = this.getApplicationContext();
        Intent i10 = new Intent(ctx10, ucrimereport.class);
        tabSpec10.setContent(i10);
        mTabHost.addTab(tabSpec10);

2)这是我正在制作和调用画廊的课程

public class ucrimereport extends Activity implements OnClickListener,OnItemSelectedListener 
    private static final int IMAGE = 1;

@Override   

    public void onCreate(Bundle savedInstanceState) 
    
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ureport);
.........
         gallary = (ImageButton)findViewById(R.id.sdpic_button);

gallary.setOnClickListener(new OnClickListener() 
        public void onClick(View v) 
            // gallary
            Intent gallery = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
            startActivityForResult(gallery, IMAGE);

        
    );

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) 
      super.onActivityResult(requestCode, resultCode, data);

      if(resultCode==RESULT_OK && requestCode==IMAGE)
       Uri selectedImage=data.getData();
       String path=getPath(selectedImage);

       Bitmap bitmapImage=BitmapFactory.decodeFile(path);
       //ImageView image=(ImageView)findViewById(R.id.image);
       image.setImageBitmap(bitmapImage);

      
    

    public String getPath(Uri uri)
      String[] filePathColumn=MediaStore.Images.Media.DATA;

      Cursor cursor=getContentResolver().query(uri, filePathColumn, null, null, null);
      cursor.moveToFirst();
      int columnIndex=cursor.getColumnIndex(filePathColumn[0]);

      return cursor.getString(columnIndex);
    

【问题讨论】:

【参考方案1】:

请尝试该代码。

public class ImageGalleryDemoActivity extends Activity     
    private static int RESULT_LOAD_IMAGE = 1;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Button buttonLoadImage = (Button) findViewById(R.id.buttonLoadPicture);
        buttonLoadImage.setOnClickListener(new View.OnClickListener() 

            @Override
            public void onClick(View arg0) 

                Intent i = new Intent(
                        Intent.ACTION_PICK,
                        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

                startActivityForResult(i, RESULT_LOAD_IMAGE);
            
        );
     
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) 
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == RESULT_LOAD_IMAGE && 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();

            ImageView imageView = (ImageView) findViewById(R.id.imgView);
            imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
        
    

【讨论】:

像梦一样工作,你能告诉我如何从相机获取图像,即当我打开相机时如何获取图像? 试试这个链接:iamvijayakumar.blogspot.in/2011/08/…

以上是关于画廊 OnActivityResult 在 TabActivity 中不起作用的主要内容,如果未能解决你的问题,请参考以下文章

片段android中未调用onActivityResult [重复]

android使用片段从相机或画廊获取照片[重复]

从图库中选择照片(onActivityResult()问题 - 我想是这样) - 片段Android

Android Photo Gallery不会从OnActivityResult中的数据返回正确的Uri

仿百度壁纸client——完结篇之Gallery画廊实现壁纸预览已经项目细节优化

Android权限错误即使在manifest.xml中声明