android - 从 android studio 的图库中选择一张图片?

标签 android styles

有人可以告诉我问题是什么,它不起作用,所以请快速帮助我真正需要:

 imagePick.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent();
            intent.setType("image/*");
            intent.setAction(Intent.ACTION_GET_CONTENT);
            startActivityForResult(Intent.createChooser(intent, "Select Contact Image"),1);
        }
    });


  public void onActivityResult(int reqCode, int resCode, Intent data)
{
    if(resCode==RESULT_OK)
    {
        if(reqCode==1) {
            imageURI=data.getData();
            iv.setImageURI(data.getData());

        }
    }
}

最佳答案

这对我有用。

private final static int SELECT_PHOTO = 12345;

 imagePick.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
            photoPickerIntent.setType("image/*");
            startActivityForResult(photoPickerIntent, SELECT_PHOTO);
        }
    });    

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

        // Here we need to check if the activity that was triggers was the Image Gallery.
        // If it is the requestCode will match the LOAD_IMAGE_RESULTS value.
        // If the resultCode is RESULT_OK and there is some data we know that an image was picked.
        if (requestCode == SELECT_PHOTO && resultCode == RESULT_OK && data != null) {
            // Let's read picked image data - its URI
            Uri pickedImage = data.getData();
            // Let's read picked image path using content resolver
            String[] filePath = { MediaStore.Images.Media.DATA };
            Cursor cursor = getContentResolver().query(pickedImage, filePath, null, null, null);
            cursor.moveToFirst();
            String imagePath = cursor.getString(cursor.getColumnIndex(filePath[0]));

            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inPreferredConfig = Bitmap.Config.ARGB_8888;
            Bitmap bitmap = BitmapFactory.decodeFile(imagePath, options);
            imageView.setImageBitmap(bitmap);

             // Do something with the bitmap


            // At the end remember to close the cursor or you will end with the RuntimeException!
            cursor.close();
        }
    }

关于android - 从 android studio 的图库中选择一张图片?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30004658/

相关文章:

android - 如何将我的 Android 手机屏幕投影到我的电脑显示器上?

c++ - QListView的自定义装饰

Android:通过 Activity 移动

安卓工作室 : auto build like Eclipse

android - Nearby API MessageListener 不触发回调

java.lang.RuntimeException : Parcelable encountered IOException writing serializable object in Android passing ArrayList object 错误

Angular2 innerHtml 绑定(bind)删除样式属性

android - 使用 ActionBarSherlock 设置 tabBar-indicator 样式

wpf - 我的 WPF 样式 setter 可以使用 TemplateBinding 吗?

.net - 为什么我不能在事件触发器中放置一个 Setter