Java 从整个手机上传图像

Java 从整个手机上传图像,java,android,image,android-layout,android-activity,Java,Android,Image,Android Layout,Android Activity,我创建了一个活动,点击按钮后,用户可以从他们的设备库上传图片,这将被转换到imageview。问题是它只能从设备库中检索图像,而不能从手机摄像头库或手机谷歌硬盘文件夹中检索图像。我希望它能够上传任何图片从他们的手机,无论来源 代码如下: Button buttonLoadImage = (Button) findViewById(R.id.btnPictureSelect); buttonLoadImage.setOnClickListener(new View.OnClickL

我创建了一个活动,点击按钮后,用户可以从他们的设备库上传图片,这将被转换到imageview。问题是它只能从设备库中检索图像,而不能从手机摄像头库或手机谷歌硬盘文件夹中检索图像。我希望它能够上传任何图片从他们的手机,无论来源

代码如下:

    Button buttonLoadImage = (Button) findViewById(R.id.btnPictureSelect);
    buttonLoadImage.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View arg0) {
            Intent i = new Intent(
                    Intent.ACTION_PICK,
                    android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            startActivityForResult(i, RESULT_LOAD_IMAGE);
        }
    });

} 

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

        if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK
                && null != data) {
            Uri selectedImage = data.getData();
            String[] filePathColumn = { MediaStore.Images.Media.DATA };

            Cursor cursor = getContentResolver().query(selectedImage,
                    filePathColumn, null, null, null);
            cursor.moveToFirst();

            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            String picturePath = cursor.getString(columnIndex);
            cursor.close();

            ImageView imageView = (ImageView) findViewById(R.id.profilePicturePreview);
            imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));

        }

    }

    private byte[] readInFile(String path) throws IOException {
        // TODO Auto-generated method stub
        byte[] data = null;
        File file = new File(path);
        InputStream input_stream = new BufferedInputStream(new FileInputStream(
                file));
        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
        data = new byte[16384]; // 16K
        int bytes_read;
        while ((bytes_read = input_stream.read(data, 0, data.length)) != -1) {
            buffer.write(data, 0, bytes_read);
        }
        input_stream.close();
        return buffer.toByteArray();

    }
任何帮助都将不胜感激

更新:

private static final int READ_REQUEST_CODE = 42;
代码中的

  Button buttonLoadImage = (Button) findViewById(R.id.btnPictureSelect);
    buttonLoadImage.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View arg0) {
            Intent imageIntent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
            imageIntent.addCategory(Intent.CATEGORY_OPENABLE);
            imageIntent.setType("image/*");
            imageIntent.setAction(Intent.ACTION_GET_CONTENT);
            startActivityForResult(imageIntent , READ_REQUEST_CODE);
        }
    });

} 

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

        if (requestCode == READ_REQUEST_CODE && resultCode == Activity.RESULT_OK) {

            Uri uri = null;
            if (data != null) {
                uri = data.getData();
                Log.i(TAG, "Uri: " + uri.toString());
                showImage(uri);
            }
        }
    }

    private void showImage(Uri uri) {
        // TODO Auto-generated method stub
          ImageView imageView = (ImageView) findViewById(R.id.profilePicturePreview);
        imageView.setImageUri(Uri.parse(new File("path_to_your_image".toString()));
    }
错误:

The method parse(String) in the type Uri is not applicable for the arguments (File)
The method setImageUri(Uri) is undefined for the type ImageView
一致

setImageUri(Uri.parse(新文件(“path_to_your_image.toString()))

更新2:

private void showImage(Uri uri) {
    // TODO Auto-generated method stub
      ImageView imageView = (ImageView) findViewById(R.id.profilePicturePreview);
       imageView.setImageUri(Uri.fromFile(new File("/sdcard/myimage.jpg")));
错误:

The method parse(String) in the type Uri is not applicable for the arguments (File)
The method setImageUri(Uri) is undefined for the type ImageView

最好使用Intent搜索设备中可用的图像类型文件

Intent imageIntent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
imageIntent.addCategory(Intent.CATEGORY_OPENABLE);
imageIntent.setType("image/*");
imageIntent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(imageIntent , READ_REQUEST_CODE); // set READ_REQUEST_CODE to 42 in your class
现在,在
onActivityResult()方法中处理结果

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

if (requestCode == READ_REQUEST_CODE && resultCode == Activity.RESULT_OK) {

        Uri uri = null;
        if (data != null) {
            uri = data.getData();
            Log.i(TAG, "Uri: " + uri.toString());
            showImage(uri);
        }
}

非常感谢。出于好奇,我应该在哪里引用代码imageview imageview=(imageview)findViewById(R.id.profilePicturePreview);imageview.setImageBitmap(BitmapFactory.decodeFile(picturePath));/A您可以使用setImage(uri)方法在imageview.imageview.setImageUri(uri.parse)中显示图像(新文件(“path_to_your_image.toString());再次感谢您的及时响应。我在eclipse中遇到了一个小错误“类型Uri中的方法解析(字符串)不适用于参数(文件)”.为了您的礼貌,我在我的第一篇文章下面增加了一个更新部分。如果您能仔细查看,我将不胜感激。任何建议都将不胜感激