从整个手机上传图片

时间:2014-10-20 15:40:01

标签: java android image android-layout android-activity

我创建了一个活动,按下按钮后用户可以从他们的设备库中上传图片,这将被投射到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)

在行

imageView.setImageUri(Uri.parse(new file(" 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 setImageUri(Uri) is undefined for the type ImageView

1 个答案:

答案 0 :(得分:1)

最好使用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);
        }
}