开发者

Android load gallery onclick, return image as bitmap

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?


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


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜