在Android N中始终将本地图像作为零字节返回

时间:2017-07-13 08:42:18

标签: android android-file android-fileprovider android-7.0-nougat

为Android N设置文件共享,如下所示

<uses-permission 
android:name="android.permission.READ_EXTERNAL_STORAGE"/>

<application
    ....>
    <provider
        android:name="android.support.v4.content.FileProvider"
        android:authorities="${applicationId}.provider"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/provider_paths"/>
</application>

in xml

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="external_files" path="."/>
</paths>

我从图库中选择图片,因此编写代码从图库中选择图像并从URI获取图像文件。

private void choosePictureIntent() throws IOException {
    Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
    intent.setType("image/*");
    intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

    // Create the File where the photo should go
    File photoFile = null;
    try {
        photoFile = createImageFile();
    } catch (IOException ex) {
        // Error occurred while creating the File
        return;
    }
    // Continue only if the File was successfully created
    if (photoFile != null) {

        Uri photoURI = FileProvider.getUriForFile(MainActivity.this,
                BuildConfig.APPLICATION_ID + ".provider",
                createImageFile());
        intent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
        startActivityForResult(intent, REQUEST_PHOTO_GALLERY);
    }
}

创建图像文件的方法

private File createImageFile() throws IOException {
    // Create an image file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    String imageFileName = "JPEG_" + timeStamp + "_";
    File storageDir = new File(Environment.getExternalStoragePublicDirectory(
            Environment.DIRECTORY_DCIM), "Camera");
    File image = File.createTempFile(
            imageFileName,  /* prefix */
            ".jpg",         /* suffix */
            storageDir      /* directory */
    );

    // Save a file: path for use with ACTION_VIEW intents
    mCurrentPhotoPath = image.getAbsolutePath();
    return image;
}

在onActivityResult

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK) {
        switch (requestCode) {
            case REQUEST_PHOTO_GALLERY:           
                Uri filePathUri = Uri.parse(mCurrentPhotoPath);
                File myFile = new File(filePathUri.getPath());
                int file_size = Integer.parseInt(String.valueOf(myFile.length()/1024));
                Log.e("path", mCurrentPhotoPath);
                Log.e("size", " " + file_size);
                try {
                    InputStream ims = new FileInputStream(myFile);

ivPreview.setImageBitmap(BitmapFactory.decodeStream(ims));

                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                    return;
                }
                break;
        }

    }
}

记录图像文件路径和文件大小

E/path: /storage/emulated/0/DCIM/Camera/JPEG_20170713_141204_1603788023.jpg

E/size:  0

我做错了什么?我必须上传图像,所以如果图像总是零字节,则无法上传图像。

0 个答案:

没有答案