【问题标题】:Upload an image to android layout [closed]将图像上传到android布局[关闭]
【发布时间】:2014-07-24 07:50:10
【问题描述】:

当单击“按钮”按钮时,它将能够浏览我的画廊文件夹中的图像,当我选择图像时,它应该被添加到我的布局中。

【问题讨论】:

    标签: android image android-layout layout image-uploading


    【解决方案1】:

    使用Intent 打开onClick 中的图库。

    Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
    photoPickerIntent.setType("image/*");
    startActivityForResult(photoPickerIntent, 1);
    

    听听结果
    onActivityResult 中获取选中的图片。使用ImageView 托管所选图像。

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    
        if (resultCode == RESULT_OK) {
            Uri photoUri = data.getData();
            if (photoUri != null) {
                try {
                    currentImage = MediaStore.Images.Media.getBitmap(this.getContentResolver(), photoUri);
                    imageView.setImageBitmap(currentImage);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
    

    【讨论】:

    • 使用 ImageView。请注意大写的“I”。
    【解决方案2】:

    当你调用画廊意图时,使用 startActivityForResult() 并且你在 onActivityResult() 中处理它

    这里是官方文档的链接:http://developer.android.com/training/basics/intents/result.html

    【讨论】:

      【解决方案3】:

      在按钮的 OnClickListener 中:

      Intent intent = new Intent();  
      intent.setType("image/*");  
      intent.setAction(Intent.ACTION_GET_CONTENT);  
      startActivityForResult(Intent.createChooser(intent, "Choose an Image"), 0);
      

      然后,覆盖 onActivityResult 并执行:

      URI uri = intent.getData()
      

      最后,从你希望图片所在的 imageview 调用 setImageURI(uri):

      ImageView iv = (ImageView) findViewById(R.id.myimageviewid);
      iv.setImageURI(uri); 
      

      【讨论】:

        猜你喜欢
        • 2012-04-03
        • 2019-12-10
        • 2012-01-13
        • 2014-06-09
        • 1970-01-01
        • 1970-01-01
        • 2016-04-24
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多