【问题标题】:how to get the images from device in android java application如何在android java应用程序中从设备获取图像
【发布时间】:2010-02-09 06:27:34
【问题描述】:

在我的应用程序中,我想上传图片。

为此,我必须从 android 设备的图库中获取图像。

我如何编写代码来实现这一点?

【问题讨论】:

    标签: android


    【解决方案1】:

    Raise an Intent with Action as ACTION_GET_CONTENT 并将类型设置为“image/*”。这将启动照片选择器活动。当用户选择图片时,可以使用onActivityResult回调来获取结果。

    类似:

    Intent photoPickerIntent = new Intent(Intent.ACTION_GET_CONTENT);
    photoPickerIntent.setType("image/*");
    startActivityForResult(photoPickerIntent, 1);
    
    protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK)
        {
            Uri chosenImageUri = data.getData();
    
            Bitmap mBitmap = null;
            mBitmap = Media.getBitmap(this.getContentResolver(), chosenImageUri);
            }
    }
    

    【讨论】:

    • 嗨 samuh。通过使用此代码,我从媒体获取图像我的问题是我需要将该图像存储在远程服务器中。当我尝试发送字节数组时,我将 mBitmap 转换为字节数组显示 5 位代码我感觉它是字节数组的地址。你能帮我解决这个问题吗提前谢谢阿斯旺
    • 您可能发送的是字节数组的 HashCode 而不是数组元素。
    • in = new FileInputStream("/sdcard/pictures/einstein3.jpg"); buf = new BufferedInputStream(in,2000); System.out.println("1......."+buf); byte[] byteArray=new byte[buf.available()]; buf.read(byteArray);现在我将“byteArray”发送到不只发送哈希码的服务器。你能建议我在哪里出错
    • 在 Android 4.4 上,这现在有不需要的行为(启动最近的文档屏幕)。看到这个答案:stackoverflow.com/a/2507973/276949
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多