【问题标题】:Android load gallery onclick, return image as bitmapAndroid加载图库onclick,将图像作为位图返回
【发布时间】:2011-10-04 23:11:30
【问题描述】:

我在从活动返回信息时遇到困难,我对实施的理解不完整。

我想要的是用户能够单击一个按钮来加载 android 库,选择一个图像,该图像(或可能是图像的链接)被转换为一个位图/可绘制的,它出现在我的活动的 UI 中布局。

我打开了 android 图库,但我没有收到任何回复(我知道为什么,图库应用中没有意图 - 我无权访问以进行编辑,但我不知道解决方案)

 ImageView galleryClick = (ImageView)findViewById(R.id.addgallery);

    profilePic = (ImageView)findViewById(R.id.profilepic);

    galleryClick.setOnClickListener(new OnClickListener(){

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent intent = new Intent(Intent.ACTION_GET_CONTENT);  
            intent.setType("image/*");

            startActivityForResult(Intent.createChooser(intent, "Select Picture"),1);

        }

    });

我希望 onActivityFinished 会在我的手写活动中被调用,但该方法从未被调用(我在其代码中放置了一个断点

 public void onActivityResult(int requestCode, int resultCode, Intent data) { 

   if (resultCode == RESULT_OK) {

       if (requestCode == 1) {
          // currImageURI is the global variable I'm using to hold the content:// URI of the image
          //currImageURI = data.getData();
       }
    }
}

解决方案?

【问题讨论】:

  • 断点并不总是受到尊重,具体取决于您从 IDE 启动应用程序的方式。在 requestCode==1 块中抛出一条日志消息 - that 出现了吗?

标签: android android-activity gallery


【解决方案1】:

如果我正确理解了您的问题,我相信之前在 Stack Overflow 上也有人问过类似的问题。这是我正在考虑的一个:

Get/pick an image from Android's built-in Gallery app programmatically

【讨论】:

  • 但我的 onActivityResult 函数永远不会被调用。正如我所提到的,我在那里放置了一个断点,它永远不会对画廊中选择的内容做任何事情
  • 尝试使用startActivityForResult(Intent.createChooser(intent, "Select Picture"), -1); 而不是startActivityForResult(Intent.createChooser(intent, "Select Picture"), 1);
  • 这更接近我的问题,stackoverflow.com/questions/2497205/…
【解决方案2】:

使用这个:

 ImageView user_Image = (ImageView) headerView.findViewById(R.id.user_image);

user_Image.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            openImageChooser();
        }
    });

private void openImageChooser() {
    Intent intent = new Intent();
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(Intent.createChooser(intent, "Select Picture"), SELECT_PICTURE);
}

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK)
    {
        if (requestCode == SELECT_PICTURE)
        {
            // Get the url from data
            Uri selectedImageUri = data.getData();
            if (null != selectedImageUri)
            {
                // Get the path from the Uri
                String path = getPathFromURI(selectedImageUri);
                Log.i(TAG, "Image Path : " + path);
                // Set the image in ImageView
                ((ImageView) findViewById(R.id.user_image)).setImageURI(selectedImageUri);
            }
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-29
    • 1970-01-01
    • 2013-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多