2011-06-30 31 views
5

我試圖從設備的/ data文件夾附加文件。如何將文件附加到設備的/ data文件夾的電子郵件

我已成功在/ data文件夾中創建「abc.txt」,我可以在該位置看到該文件。

我使用下面的代碼發送電子郵件:

Intent intent = new Intent(Intent.ACTION_SEND); 
    intent.setType("text/plain"); 
    intent.putExtra(Intent.EXTRA_EMAIL, new String[]{[email protected]}); 
    intent.putExtra(Intent.EXTRA_STREAM, 
     Uri.parse(Environment.getDataDirectory()+"/abc.txt")); 
    intent.putExtra(Intent.EXTRA_TEXT, "hello.."); 
    startActivity(Intent.createChooser(intent, email_chooser_title)); 

,但我無法接收的附件..

PL。讓我知道我做了什麼錯誤..

謝謝。

+0

你不得到任何錯誤?它只是發送,你收到電子郵件,但沒有附件? –

+0

我還沒有得到任何錯誤。它只是發送沒有任何附件。 – brig

回答

2

您必須將文件複製到外部目錄(又名SD卡)。這是因爲在電子郵件應用程序無法訪問您的數據目錄(以同樣的方式,你不能訪問其他應用程序的數據目錄)

+0

是的,如果手機處於root模式,它可能會工作。 –

+0

是的,如果在root模式下,那麼你可以從/ data /文件夾讀取文件...否則你必須使用/ sdcard – Cristian

+0

這在技術上並非如此;你可以把一個世界可讀的文件*放在你的應用程序的文件夾中*在/ data下,或者在那個特定的android設備提供內部存儲和一個電子郵件應用程序的應用程序中,應該能夠讀取它,儘管已經有拒絕的gmail應用程序版本要做到這一點(即使操作系統會讓他們),除非指出有一些/../../類型的路徑欺騙。 –

0

試試這個,而不是4號線

intent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:/data/abc.txt")); 

如果是在SD卡:

intent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:/sdcard/data/abc.txt")); 
+0

注意第一個不起作用,因爲相關應用程序沒有權限在/ data下寫入_directly_,但只能在/ data中的已分配子文件夾中寫入任何內容。另一個應用程序的讀取能力將基於創建該文件的人設置的權限,但在gmail應用程序中,_ability_和_willingness_的讀取方式並不完全相同。 –

0

而不是使用Environment.getDataDirectory()+"/abc.txt"的你嘗試使用Environment.getExternalStorageDirectory()+"/abc.txt"

我覺得這個改變應該可以做到。當然,爲了這個工作,你必須在手機/模擬器的SD卡上有文件。

1

嘗試使用此代碼:

File sdCard = Environment.getExternalStorageDirectory(); 
PATH=sdCard.toString()+"/abc.txt"; 
Intent intent=new Intent(Intent.ACTION_SEND); 
intent.setType("**/**");// or intent.setType("text/plain"); 
String[] recipients={"[email protected]"); 
intent.putExtra(Intent.EXTRA_EMAIL, recipients); 
intent.putExtra(Intent.EXTRA_SUBJECT,getResources().getString(R.string.app_name)); 
intent.putExtra(Intent.EXTRA_STREAM,Uri.parse("file://"+PATH)); 
intent.putExtra(Intent.EXTRA_TEXT, "hello.."); 
startActivity(Intent.createChooser(intent,"")); 

確保有一個在您的設備/模擬器的SD卡的abc.txt文件。

也包括在項目的清單文件這兩個權限:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 
<uses-permission android:name="android.permission.INTERNET"/> 
+0

它工作嗎? – AkashG

0
Try this code!!!!  


File sdCard = Environment.getExternalStorageDirectory(); 
PATH=sdCard.toString(); 
String path =PATH.replace("/mnt","") + "/abc.txt"; 
Intent intent=new Intent(Intent.ACTION_SEND); 
intent.setType("**/**");// or intent.setType("text/plain"); 
String[] recipients={"[email protected]"); 
intent.putExtra(Intent.EXTRA_EMAIL, recipients); 
intent.putExtra(Intent.EXTRA_SUBJECT,getResources().getString(R.string.app_name)); 
intent.putExtra(Intent.EXTRA_STREAM,Uri.parse("file://" +path)); 
intent.putExtra(Intent.EXTRA_TEXT, "hello.."); 
startActivity(Intent.createChooser(intent,"")); 
1

Android: Attaching files from internal cache to Gmail(有一些修改,允許你運行棒棒糖這個代碼)。

基本上你需要實現自己的類擴展的ContentProvider(說CachedFileProvider,見下文),並在清單中添加下一個場(內/應用標籤):

<provider 
     android:name=".CachedFileProvider" 
     android:authorities="com.your.app.provider" 
     android:grantUriPermissions="true" /> 

當打開一個郵件選擇器,使用下面的代碼:

File f = getLocalFileToSend(); 
Uri uri = Uri.parse("content://" + CachedFileProvider.AUTHORITY + "/" + f.getName()); 
Intent intent = new Intent(Intent.ACTION_SEND); 
intent.setType("text/plain"); 
intent.putExtra(Intent.EXTRA_SUBJECT, "Subject"); 
intent.putExtra(Intent.EXTRA_TEXT, "Body"); 
intent.putExtra(Intent.EXTRA_STREAM, uri); 

intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); // Grant permissions to read. 
startActivity(Intent.createChooser(intent, "Send Email")); 

代碼爲CachedFileProvider:

package ...; 

import java.io.File; 
import java.io.FileNotFoundException; 

import android.content.ContentProvider; 
import android.content.ContentValues; 
import android.content.UriMatcher; 
import android.database.Cursor; 
import android.net.Uri; 
import android.os.ParcelFileDescriptor; 
import android.util.Log; 


public class CachedFileProvider extends ContentProvider { 

    private static final String TAG = "CachedFileProvider"; 

    private static final String CLASS_NAME = "CachedFileProvider"; 

    // The authority is the symbolic name for the provider class. 
    // Should match one in the manifest file. 
    public static final String AUTHORITY = "com.your.app.provider"; 

    // UriMatcher used to match against incoming requests 
    private UriMatcher uriMatcher; 

    @Override 
    public boolean onCreate() { 
     uriMatcher = new UriMatcher(UriMatcher.NO_MATCH); 

     // Add a URI to the matcher which will match against the form 
     // 'content://com.stephendnicholas.gmailattach.provider/*' 
     // and return 1 in the case that the incoming Uri matches this pattern 
     uriMatcher.addURI(AUTHORITY, "*", 1); 

     return true; 
    } 

    @Override 
    public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException { 
     Log.v(TAG, "Called with uri: '" + uri + "'." + uri.getLastPathSegment()); 

     // Check incoming Uri against the matcher 
     switch (uriMatcher.match(uri)) { 

      // If it returns 1 - then it matches the Uri defined in onCreate 
      case 1: 

       // The desired file name is specified by the last segment of the path. 
       String fileLocation = getContext().getCacheDir() + File.separator 
         + uri.getLastPathSegment(); 

       // Create & return a ParcelFileDescriptor pointing to the file 
       // Note: I don't care what mode they ask for - they're only getting 
       // read only 
       ParcelFileDescriptor pfd = ParcelFileDescriptor.open(new File(
         fileLocation), ParcelFileDescriptor.MODE_READ_ONLY); 
       return pfd; 

      // Otherwise unrecognised Uri 
      default: 
       Log.v(TAG, "Unsupported uri: '" + uri + "'."); 
       throw new FileNotFoundException("Unsupported uri: " 
         + uri.toString()); 
     } 
    } 

    // ////////////////////////////////////////////////////////////// 
    // Not supported/used/required for this example 
    // ////////////////////////////////////////////////////////////// 

    @Override 
    public int update(Uri uri, ContentValues contentvalues, String s, 
         String[] as) { 
     return 0; 
    } 

    @Override 
    public int delete(Uri uri, String s, String[] as) { 
     return 0; 
    } 

    @Override 
    public Uri insert(Uri uri, ContentValues contentvalues) { 
     return null; 
    } 

    @Override 
    public String getType(Uri uri) { 
     return null; 
    } 

    @Override 
    public Cursor query(Uri uri, String[] projection, String s, String[] as1, 
         String s1) { 
     return null; 
    } 
} 
相關問題