尝试从DCIM获取图片怪异的行为怪异、行为、图片、DCIM

2023-09-06 07:34:00 作者:旧城旧梦旧情人。

我想从我的DCIM活动中加载图片。我用下面的code:

I am trying to load a picture in my activity from DCIM. I use the following code :

int BROWSE_PICTURES = 0;
public void openBrowsePictures() {
    Intent i = new Intent(Intent.ACTION_PICK,MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

    startActivityForResult(i, BROWSE_PICTURES);
}

和中的onActivityResult:

and in onActivityResult :

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

    if (requestCode == BROWSE_PICTURES && resultCode == RESULT_OK && null != data) { // we have bitmap from filesystem!
        Uri selectedImage = data.getData();
        Log.d("CAMERA","____"+selectedImage.toString());


        String[] filePathColumn = {MediaStore.Images.Media.DATA};



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

        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        Log.d("CAMERA", " column : " + columnIndex);
        String filePath = cursor.getString(columnIndex);
        cursor.close();


        Bitmap yourSelectedImage = BitmapFactory.decodeFile(filePath);


        Log.d("CAMERA", "----" + filePath);
    }
}

当我尝试加载从文件系统中拍摄的照片,情况就变得奇怪。有用如预期时。我选择从EasyScreenshot文件,但是当B图片。我选择DCIM /相机路径图片这是行不通的。如果我运行code中的Log.d的一个。案例打印:

The situation becomes strange when I try to load a taken picture from filesystem. It works as expected when a. I choose a picture from EasyScreenshot file but when b. I choose picture from DCIM/Camera path it does not work. If I run the code the Log.d at the a. case prints :

CAMERA:____content://媒体/外部/图像/媒体/ 27487

CAMERA﹕ ____content://media/external/images/media/27487

和第二Log.d:

CAMERA:---- /存储/模拟/ 0 /图片/截图/ Screenshot_2014-12-18-15-14-22.png

CAMERA﹕ ----/storage/emulated/0/Pictures/Screenshots/Screenshot_2014-12-18-15-14-22.png

不过,在情况b它打印以下内容:

but , in case b it prints the following :

第一log.d:

CAMERA: ____content://com.google.android.apps.photos.content/0/https%3A%2F%2Flh5.googleusercontent.com%2F7tUacBA_4oYS2Q8CmkINWHa93B_n7heNyt3OyVZgkY8%3Ds0-d

电脑怎样上传照片 ww

CAMERA: ____content://com.google.android.apps.photos.content/0/https%3A%2F%2Flh5.googleusercontent.com%2F7tUacBA_4oYS2Q8CmkINWHa93B_n7heNyt3OyVZgkY8%3Ds0-d

和第二log.d:

CAMERA:----空

CAMERA﹕ ----null

我试运行中的Nexus 4设备的应用程序的Andr​​oid 5.0.1

I test the application in a nexus 4 device running Android 5.0.1

感谢您提前

推荐答案

您不能假设返回的URI媒体选择器将对应于本地文件。它看起来像你选择G +照片或其它图像,是不是在设备中。

You cannot assume that the Uri returned by the media picker will correspond to a local file. It looks like you're selecting a G+ photo or some other image that is not in the device.

正确的方法去将是使用 ContentResolver的 访问图片作为流。例如:

The correct way to go would be to use a ContentResolver to access the picture as a stream. For example:

InputStream inputStream = null;
if (ContentResolver.SCHEME_CONTENT.equals(selectedImage.getScheme())) {
    inputStream = context.getContentResolver().openInputStream(selectedImage);
} else if (ContentResolver.SCHEME_FILE.equals(selectedImage.getScheme())) {
    inputStream = new FileInputStream(selectedImage.getPath());
}

bitmap = BitmapFactory.decodeStream(inputStream);

这应为工作内容:// 文件:// 的URI

和(非常重要),请务必从后台线程(如的AsyncTask )做到这一点,否则你会得到一个 NetworkOnMainThreadException 如果URI是远程之一。

And (very important) make sure to do this from a background thread (e.g. AsyncTask), otherwise you'll get a NetworkOnMainThreadException if the uri is a "remote" one.