2011-06-30 5 views
5

デバイスの/ dataフォルダからファイルを添付しようとしています。デバイスの/ dataフォルダからファイルを電子メールに添付する方法

私は正常に "abc.txt"を/ dataフォルダに作成しましたが、その場所でファイルを見ることができます。

私は電子メールを送信するためのコードの下に使用しています..

PLを

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

が、私は、添付ファイルを受信することができません。私がした間違いが何であるか教えてください。

ありがとう。

+0

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のためのコード:次のコードを使用します何かエラーがありますか?送信するだけでメールは届きますが、添付ファイルはありませんか? –

+0

私は間違いをしていません。添付ファイルなしで送信するだけです。 – brig

答えて

2

ファイルを外部ディレクトリ(別名SDカード)にコピーする必要があります。電子メールアプリケーションは、あなたのデータディレクトリにアクセスすることはできませんので、それはそれはSDカード内にある場合は代わりに第四行

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

のこれを試してみてください(あなたが他のアプリのデータディレクトリにアクセスすることができないのと同様に)

+0

ええ、それはおそらく電話機がルートモードで動作するでしょう。 –

+0

はい、ルートモードであれば、/ data/folderからファイルを読むことができます。そうでなければ/ sdcardを使用する必要があります – Cristian

+0

これは技術的には正しいことではありません。/dataの下にあるアプリケーションのフォルダ*に、世界の読めるファイル*を置くことができます。その特定のアンドロイドデバイスが内部ストレージと電子メールアプリケーションを提供できる場所であれば、読めるようにする必要があります。 /../../タイプのパストリッキーがある場合を除いて、そうする(オペレーティングシステムがそれらを許可するにもかかわらず)。 –

0

です。

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

問題のアプリケーションは/ dataの中に_directly_を書き込む権限を持っていないため、/ data内の割り当てられたサブフォルダにのみ書き込む権限がないため、最初のアプリケーションは機能しません。別のアプリの読み込み能力は、誰でもファイルを作成したアクセス許可に基づいていますが、_ability_と_willingness_は、Gmailのアプリケーションの場合と全く同じではありません。 –

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ファイルがあることを確認してください。 (あなたはロリポップでこのコードを実行することを可能にするいくつかの修正を加えた)Android: Attaching files from internal cache to Gmailから

<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

はまた、プロジェクトのマニフェストファイルでは、これらの2つの権限が含まれます。

は基本的にあなたが(下記参照、CachedFileProviderを言う)のContentProviderを拡張する独自のクラスを実装し、マニフェストに(内部/アプリケーションタグ)を次のフィールドを追加する必要があります。

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

メールチューを開いて、あなたはいけない

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; 
    } 
} 
関連する問題