-1

为了尽可能简短,我使用以下代码将文件上传到 DropBox:

PackageManager manager = getPackageManager();
                                File f = new File(getContext().getExternalFilesDir("diabetix"),"TESTINTENT.xml");
                                Intent intent = new Intent(Intent.ACTION_SEND);
                                intent.setType("text/*");
                                intent.setPackage("com.dropbox.android");
                                intent.putExtra(Intent.EXTRA_STREAM, FileProvider.getUriForFile(getContext(),"david.projectclouds.MainActivity",f));
                                startActivity(Intent.createChooser(intent, "title"));

意图很好地打开,但它不保存文件并输出错误(不在日志中,保管箱应用程序会这样做)。

由于我在 OneDrive 上遇到了类似的问题,因此我决定打印出我的文件 URI(OneDrive 出现 NoFileSpecified 错误)。

内容://david.projectclouds.MainActivity/file/diabetix/Temp.xml

这是我的 URI。我看了一些例子,似乎它是正确的。该文件在使用 FileExplorer 应用程序时也存在。

我用它来显示权限:

    public void requestReadWritePermissions(){
 // Here, thisActivity is the current activity
    if (ContextCompat.checkSelfPermission(this,
            Manifest.permission.READ_EXTERNAL_STORAGE)
            +ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)!=PackageManager.PERMISSION_GRANTED) {

        if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                Manifest.permission.READ_EXTERNAL_STORAGE)||(ActivityCompat.shouldShowRequestPermissionRationale(this,
                Manifest.permission.WRITE_EXTERNAL_STORAGE))) {

            // Show an explanation to the user *asynchronously* -- don't block
            // this thread waiting for the user's response! After the user
            // sees the explanation, try again to request the permission.
            Toast.makeText(this, "App needs this permission to run", Toast.LENGTH_SHORT);
        }else{
                ActivityCompat.requestPermissions(this,
                        new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                        REQUEST_WRITE_PERMISSIONS);
            System.out.println("PERMISSION_GRANTED");

            }
        } else {


            // No explanation needed, we can request the permission.

            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
                    REQUEST_READ_PERMISSIONS);

        }
    }

相同的代码在 7.0 以下的手机上运行良好(认为它是 5.0)。

有任何想法吗?有人可以查看我的权限吗?删除它们会输出相同的错误。它只请求一个“请求访问媒体、文件、照片..”是否应该显示“写入媒体等”?也?

4

2 回答 2

1

你捕捉到 requestPermission 结果了吗?当您调用 requestPerimission 时,您还需要覆盖 onRequestPermissionsResult,如下所示:

public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (requestCode) {
            case PERMISSION_REQUEST_CODE: {
                // If request is cancelled, the result arrays are empty.
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    //granted. here you need to call the method, which uploads file to DropBox
                }
                else{
                    // no granted. Handle this somehow.     
                }
                break;
            }

        }
    }

请阅读教程:

于 2017-06-27T13:09:28.963 回答
1

对于 Android 7.0+,有一个新策略。你必须使用一个FileProvider. 有关更多详细信息,请参见此处

简而言之,您将在文件夹中定义它并在文件夹下AndroidManifest.xml创建一个新的(可能需要创建一个)。在那里您定义应该与其他应用程序共享的路径。resources.xmlxml

从文档

<manifest>
    ...
    <application>
        ...
        <provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="com.mydomain.fileprovider"
            android:exported="false"
            android:grantUriPermissions="true">
            ...
        </provider>
        ...
    </application>
</manifest>

文件夹下paths.xml的。xml

<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path name="external_files" path="."/>
</paths>

要获取新的 URI,您可以使用这样的#FileProvider.getUriForFile方法

FileProvider.getUriForFile(yourContext,
        "your.package.name" + ".provider",
        yourFile);

URI 看起来像这样

content:///fileprovider/pathname/actualPathToFile

最后,您必须将标志添加Intent.FLAG_GRANT_READ_URI_PERMISSION)到您的Intent. 而已。

于 2017-06-27T13:09:29.517 回答