Android onclick加载图库,以位图形式返回图像

发布于 2024-12-08 11:36:54 字数 1137 浏览 1 评论 0原文

我很难从活动返回信息,我对实现的理解不完整。

我想要的是用户能够单击按钮来加载 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();
       }
    }
}

解决方案中放置了断点?

I am having difficulty returning information from an activity, my understanding of the implementation is incomplete.

What I want is the user to be able to click a button to load up the android gallery, select an image, that image (or link to the image perhaps) is converted to a bitmap/drawable which appears in my activity's UI layout.

I have the android gallery opening but I am not getting any response back from it (I know why, there are no intents in the gallery app - which I dont have access to in order to edit, but I dont know the solution)

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

        }

    });

What I was hoping was the onActivityFinished would be called in my handwritten activity, but that method is never called (I put a breakpoint in its code

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

solutions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

骄傲 2024-12-15 11:36:54

如果我正确理解你的问题,我相信之前在 Stack Overflow 上也有人问过类似的问题。这是我想到的:

获取/挑选以编程方式获取 Android 内置图库应用中的图像

If I understand your question correctly, I believe a similar question has been asked previously on Stack Overflow. Here's the one I'm thinking of:

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

凌乱心跳 2024-12-15 11:36:54

使用这个:

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

Use this :

 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);
            }
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文