使用getContentResolver()打开文件提供程序内容Uri.openInputStream(uri)始终在输入流中返回-1

时间:2017-01-10 09:25:32

标签: android xml uri android-fileprovider

在android清单中,在应用程序标记

中添加了FileProvider
<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"/>
    </provider>
  </application>

这是xml provider_paths

 <?xml version="1.0" encoding="utf-8"?>
<paths>
   <external-path name="files" path="."/>
 </paths>

这是如何启动IMAGE CAPTURE意图:

     public void imageCapture() {
    try{
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        String imageFileName = "JPEG_" + timeStamp + "_" + ".jpeg";

      //Here file is created with the imageFileName 
        photoFile = FileClientService.getFilePath(imageFileName, getApplicationContext(), "image/jpeg"); 

       //Content URI for file 
        capturedImageUri = FileProvider.getUriForFile(this,getApplicationContext().getPackageName()+".provider",photoFile);

        Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, capturedImageUri);

        if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.LOLLIPOP) {
            cameraIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
        } else if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.JELLY_BEAN) {
            ClipData clip=
                    ClipData.newUri(getContentResolver(), "a Photo", capturedImageUri);

            cameraIntent.setClipData(clip);
            cameraIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);

        } else {
            List<ResolveInfo> resInfoList=
                    getPackageManager()
                            .queryIntentActivities(cameraIntent, PackageManager.MATCH_DEFAULT_ONLY);

            for (ResolveInfo resolveInfo : resInfoList) {
                String packageName = resolveInfo.activityInfo.packageName;
                grantUriPermission(packageName, capturedImageUri,
                        Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
            }
        }

        if (cameraIntent.resolveActivity(getApplicationContext().getPackageManager()) != null) {
            if (photoFile != null) {
                startActivityForResult(cameraIntent, MultimediaOptionFragment.REQUEST_CODE_TAKE_PHOTO);
            }
        }

    }catch (Exception e){
        e.printStackTrace();
    }
}

这是为写入此文件而创建的文件

    /storage/emulated/0/xyz/image/JPEG_20170110_141532_.jpeg

我在onActivityResult中获得的内容Uri:

content://com.example.sample.provider/files/xyz/image/JPEG_20170110_141532_.jpeg

这是onAcivityResult方法

   public void onActivityResult(int requestCode, int resultCode, Intent intent) {
    try {
        if ((requestCode == REQUEST_CODE_ATTACH_PHOTO ||
                requestCode == REQUEST_CODE_TAKE_PHOTO)
                && resultCode == Activity.RESULT_OK) {
            Uri selectedFileUri = (intent == null ? null : intent.getData());
            File file = null;
            if (selectedFileUri == null) {
                selectedFileUri = ((ConversationActivity) fragmentActivity).getCapturedImageUri();
                file = ((ConversationActivity) fragmentActivity).getFileObject();
            }

            if (selectedFileUri == null) {
                Bitmap photo = (Bitmap) (intent != null ? intent.getExtras().get("data") : null);
                selectedFileUri = ImageUtils.getImageUri(fragmentActivity, photo);
            }
            if (selectedFileUri != null) {
                file = ((ConversationActivity) fragmentActivity).getFileObject();
            }
            getConversationFragment().loadFile(selectedFileUri, file);
            Log.i(TAG, "File uri: " + selectedFileUri);
        }
    } catch (Exception e) {
    }
}

这是我用来打开内容URI的方法,我有内容URI和要写的文件

   public  void writeFile(Uri uri,File file) {
    InputStream in = null;
    OutputStream out = null;
    try {
         in = context.getContentResolver().openInputStream(uri);//I have context object
        byte[] buffer = new byte[1024];
        int bytesRead = -1;
        out = new FileOutputStream(file);
        while ((bytesRead = in.read(buffer)) != -1) { // here in.read(buffer)) is is always -1
            out.write(buffer, 0, bytesRead);
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    } finally {
        if (in != null && out != null) {
            try {
                out.flush();
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }
}

当我签入日志时,我正面临打开内容URI的问题,该内容URI用于写入上面 writeFile 方法创建的输入流始终返回-1 的文件 in.read(buffer))返回-1

如何解决这个问题。 任何帮助表示赞赏

0 个答案:

没有答案
相关问题