为了尽可能简短,我使用以下代码将文件上传到 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)。
有任何想法吗?有人可以查看我的权限吗?删除它们会输出相同的错误。它只请求一个“请求访问媒体、文件、照片..”是否应该显示“写入媒体等”?也?