2017-06-27 50 views
-1

要尽可能的短,我用下面的代码文件上传到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)。

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

回答

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; 
      } 

     } 
    } 

请阅读this教程:

+0

是的我在我的代码中有这个方法。 – Nephilim

1

使用的是Android 7.0或更高版本有一个新的政策。你必须使用FileProviderSee here了解更多详情。

总之,您可以在AndroidManifest.xml中定义它并在xml文件夹下创建一个新的resources.xml(可能需要创建一个)。在那里你定义了应该与其他应用共享的路径。

从文档

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

xml文件夹下的paths.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。而已。

+0

谢谢你的时间。正如你在我的代码中看到的,我正在使用FileProvider。但是我的paths.xml是不同的。你能告诉我我应该用什么路径=“。” ?我只是把它留在那个时期吗?如果不是,我补充什么? 我也使用intent.setFlags()在你的答案中添加该标志。但无济于事。 – Nephilim

+0

@Nephilim请参阅这里的文档https://developer.android.com/reference/android/support/v4/content/FileProvider.html。有不同的路径类型。 '。'字符正在方便。在我的例子中,它共享外部路径上的每个文件,例如SD卡。 –

+0

谢谢。我会把它留作''。 ''直到我设法解决这个错误。 – Nephilim