2011-04-30 19 views
1

我的應用程序可以選擇發送日誌,該日誌存儲在應用程序的安全存儲中。該文件的路徑是「/data/data/com.mycompany.myapp/files/log.zip」。該文件的權限已被chnged到MODE_WORLD_READABLE,並啓動電子郵件的意圖是:從我的應用程序在Gmail中的安全存儲中附加文件

Intent i = new Intent(Intent.ACTION_SEND); 
i.setType("application/zip"); 
i.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///data/data/com.mycompany.myapp/files/log.zip)); 
startActivity(Intent.createChooser(i, "Send Error Log")); 

如果該文件位於SD卡上,是沒有問題的。但是,當它是安全的存儲時,沒有附件。我非常肯定,這不是一個權限問題,因爲它可以與股票電子郵件客戶端和TouchDown Exchange應用程序完美配合。只有Gmail有這個問題。這是Gmail中的一個小故障,還是我錯過了一些東西?

謝謝。

+0

即可獲得流直接在文件和測試你的理論呢? – MJB 2011-05-01 04:33:16

+1

我知道一個事實,即流是可訪問的 - 其他電子郵件應用程序可以訪問它並附加它沒有問題 – user496854 2011-05-02 06:24:32

回答

1

沒關係,我找到了答案 - Gmail不允許任何附件,除非它們來自SD卡。我最終不得不將文件複製到外部存儲器cachce,然後一切正常。它認爲Gmail會任意決定它不會在文件上使用永久存儲,即使權限是正確的!

+1

任何人都可以找到解決此問題的另一種方法? (如果他們沒有和SD卡怎麼辦?) – TChadwick 2012-10-19 20:22:19

+1

@TChadwick看看這個答案http:// stackoverflow。com/a/18332000/1082344我能夠使用支持庫中的FileProvider類將文件從應用的安全存儲附加到Gmail,並手動授予和撤銷文件的權限。 – IsaacCisneros 2014-02-28 11:01:52

+1

這是不正確的。使用FileProvider,您可以通過Gmail分享。 – neteinstein 2015-01-23 18:10:27

0

Gmail客戶端應用程序附件現在可以在Android 4.0及更高版本的設備上運行。我已經在我的4.2和4.3設備上進行了測試,它們都能正常工作

我已經使用我的moto razr 2.3驗證過Gmail客戶端無法工作。使用另一個郵件客戶端工作得很好,所以你的答案截至2011年是正確的。

2

是的,我們可以將使用FileProvider.Using FileProvider存儲在文件目錄到Gmail內部文件,我們可以給我們的一些應用程序的內部文件(如在filepaths.xml提到)

臨時訪問在表現爲Android的文件中所提及添加FileProvider:

<provider 
    android:name="android.support.v4.content.FileProvider" 
    android:authorities="com.package.name.fileprovider" 
    android:grantUriPermissions="true" 
    android:exported="false"> 
    <meta-data 
     android:name="android.support.FILE_PROVIDER_PATHS" 
     android:resource="@xml/filepaths" /> 
</provider> 
在應用程序的資源

現在/ XML文件夾中創建filepaths.xml,並添加以下代碼:

<paths> 
<files-path path="." name="name" /> 

注:這將使訪問根目錄的文件目錄,如果你想給一些子目錄特定的訪問,說圖片,在你的內部存儲提到的路徑爲「圖像/」

<paths> 
    <files-path path="images/" name="name" /> 

在代碼:

File file=new File(context.getFilesDir(),"test.txt"); 

Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND_MULTIPLE); 

shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, 
           "Test"); 

shareIntent.setType("text/plain"); 

shareIntent.putExtra(android.content.Intent.EXTRA_EMAIL, 
          new String[] {"email-address you want to send the file to"}); 

Uri uri = FileProvider.getUriForFile(context,"com.package.name.fileprovider", 
               file); 

      ArrayList<Uri> uris = new ArrayList<Uri>(); 
      uris.add(uri); 

      shareIntent .putParcelableArrayListExtra(Intent.EXTRA_STREAM, 
                uris); 


      try { 
       context.startActivity(Intent.createChooser(shareIntent , "Email:").addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));              


      } 
      catch(ActivityNotFoundException e) { 
       Toast.makeText(context, 
           "Sorry No email Application was found", 
           Toast.LENGTH_SHORT).show(); 
      } 
     } 

這個工作對me.Hope這有助於:)

0

AndroidManifest.xml中

 <provider 
     android:name="android.support.v4.content.FileProvider" 
     android:authorities="com.cummins.faultadvisor.LogFileShare" 
     android:exported="false" 
     android:grantUriPermissions="true" > 
     <meta-data 
      android:name="android.support.FILE_PROVIDER_PATHS" 
      android:resource="@xml/log_file_provider" /> 
    </provider> 

RES/XML/path.xml

 <?xml version="1.0" encoding="utf-8"?> 
    <paths xmlns:android="http://schemas.android.com/apk/res/android"> 
    <files-path name="files" path=""/> 
    </paths> 

您的活動文件應具有以下意圖:

Uri log_file_uri = null; 
    Uri logcat_file_uri = null; 
    String log_filePath = activity.getApplicationContext().getFilesDir().getPath() 
    + "/"+ activity.getApplicationContext().getResources().getString(R.string.log_file); 
    String logcat_filePath = activity.getApplicationContext().getFilesDir() 
    .getPath()+ "/"+ activity.getApplicationContext().getResources().getString(R.string.logcat_file); 
    File log_file = new File(log_filePath);File logcat_file = new File(logcat_filePath); 
    if (log_file.exists() && logcat_file.exists()) { 
    log_file_uri = FileProvider.getUriForFile(
    activity.getApplicationContext(),"com.example.LogFileShare",log_file); 
    logcat_file_uri = FileProvider.getUriForFile(
    activity.getApplicationContext(),"com.example.LogFileShare",logcat_file); 
相關問題