Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/192.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android file.canRead()=选择图像时为false,但拍照时为false_Android_Image - Fatal编程技术网

Android file.canRead()=选择图像时为false,但拍照时为false

Android file.canRead()=选择图像时为false,但拍照时为false,android,image,Android,Image,我在“上传图像”页面上有两个按钮,供用户将图像上传到web服务。一个用于选择设备上的图像,另一个用于使用相机拍照 thisFragment.findViewById(R.id.btnChooseImage).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Assert.assertNotNull("file uri

我在“上传图像”页面上有两个按钮,供用户将图像上传到web服务。一个用于选择设备上的图像,另一个用于使用相机拍照

thisFragment.findViewById(R.id.btnChooseImage).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Assert.assertNotNull("file uri not null before firing intent", mFileUri);
            Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            //this is the file that the camera app will write to
            intent.putExtra(MediaStore.EXTRA_OUTPUT, mFileUri);
            startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
        }
    });

    thisFragment.findViewById(R.id.btnTakePhoto).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Assert.assertNotNull("file uri not null before firing intent", mFileUri);
            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            //this is the file that the camera app will write to
            intent.putExtra(MediaStore.EXTRA_OUTPUT, mFileUri);
            startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
        }
    });
btnTakePhoto在加载ImageView时工作正常,在我拍照时显示结果,但当我使用“其他”按钮选择照片时,ImageView为空

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK) {
        if(data != null) {
            if(data.getData() != null) {
                Log.v(LOG_TAG, "intent data: " + data.getData().toString());
            }
            if(data.getAction() != null) {
                Log.v(LOG_TAG, "intent action: " + data.getAction().toString());
            }
            if(data.getExtras() != null) {
                Log.v(LOG_TAG, "intent extras: " + data.getExtras().toString());
            }
        }

        Assert.assertNotNull("file uri in onActivityResult", mFileUri);
        Log.v(LOG_TAG, "stored file name is " + mFileUri.toString());
        File file = getFileFromUri();
        if(file != null) {
            Bitmap bm = decodeSampledBitmapFromFile(file, 500, 500);
            imgMain.setImageBitmap(bm);
        }else{
            imgMain.setImageBitmap(null);
        }
    } else {
        parentActivity.finish();
    }

}

    private File getFileFromUri() {
    if(mFileUri != null) {
        try {
            URI uri;
            if(mFileUri.toString().startsWith("file://")){
                //normal path
                uri = URI.create(mFileUri.toString());
            } else {
                //support path
                uri = URI.create("file://" + mFileUri.toString());
            }
            File file = new File(uri);
            if (file != null) {
                //if (file.canRead()) {
                    return file;
                //}
            }
        } catch (Exception e) {
            return null;
        }
    }
    return null;
}
我注意到当我点击这行代码时:

if (file.canRead()) {
                    return file;
                }
拍摄照片时file.canRead()为TRUE,但选择照片时为FALSE。当我逐步查看“uri”变量的值时,它们是:

file:///storage/emulated/0/Pictures/IMG_20150916_141518.jpg - this works
file:///storage/emulated/0/Pictures/IMG_20150916_141854.jpg -这不管用

知道这是怎么回事吗

更新:尝试使用ContentResolver的InputStream方法,但位图仍然无法显示:

Uri selectedImage = data.getData();
        InputStream imageStream = null;
        try {
             imageStream = parentActivity.getContentResolver().openInputStream(selectedImage);
        }catch (FileNotFoundException e){
            Log.v(LOG_TAG, "cant load file " + mFileUri.toString());
        }

       Bitmap bm = BitmapFactory.decodeStream(imageStream);
        imgMain.setImageBitmap(bm);

因为在大多数Android 4.4 +设备上,你的动作都不会返回<代码>文件://< /COD> <代码> URI 首先,也许你可以考虑停止对文件的思考,而不是专注于使用<代码> URI 本身。在
ContentResolver
上调用
openInputStream()
以获取
InputStream
,该函数适用于常规
文件://
Uri
内容://
Uri值。
Uri.getScheme()
的值是多少?如果是文件,可能会更改
新文件(uri)
新文件(uri.getPath())将起作用。否则,您将需要按照Commonware的建议使用
ContentResolver
;在相同的结果下,file.canread()在选择文件时仍然为false。我会调查下议院的建议@Commonware你有什么资源/示例可以让我更快吗?“你有什么资源/示例可以让我更快吗?”--
getContentResolver()。openInputStream(data.getData())
@commonware仍不工作,将用我的新代码更新上面的内容