要尽可能的短,我用下面的代码文件上传到DropBox的:困难文件URI在7.0
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"));
意图开辟了很好,但它不保存文件和会输出一个错误(不在日志中,Dropbox应用程序会这样做)。
由于我与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)。
任何想法?有人可以查看我的权限吗?删除它们会输出相同的错误。它只请求一个“请求访问媒体,文件,照片..”如果它显示“写入媒体等”太?
是的我在我的代码中有这个方法。 – Nephilim