2011-06-30 6 views
5

मैं डिवाइस के डिवाइस/डेटा फ़ोल्डर से फ़ाइल संलग्न करने का प्रयास कर रहा हूं।डिवाइस के डेटा/डेटा फ़ोल्डर से ईमेल को फ़ाइल कैसे संलग्न करें

मैंने सफलतापूर्वक/डेटा फ़ोल्डर में "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

आपको फ़ाइल को बाहरी निर्देशिका (उर्फ एसडी कार्ड) में कॉपी करना होगा। यह इसलिए है क्योंकि ईमेल आवेदन (उसी तरह से है कि आप अन्य एप्लिकेशन का डेटा निर्देशिका का उपयोग नहीं कर सकते हैं) अपने डेटा निर्देशिका तक नहीं पहुँच सकता

+0

हाँ, यह संभवतः काम करेगा अगर फोन रूट मोड में है। –

+0

हां, यदि रूट मोड में है, तो आप/डेटा/फ़ोल्डर से फ़ाइलें पढ़ सकते हैं ...अन्यथा आपको/sdcard – Cristian

+0

का उपयोग करना होगा यह तकनीकी रूप से सत्य नहीं है; आप अपने एप्लिकेशन के फ़ोल्डर * में/डेटा या जहां भी वह विशेष एंड्रॉइड डिवाइस आंतरिक स्टोरेज के साथ एप्लिकेशन प्रदान करता है और एक ईमेल एप्लिकेशन _should_ इसे पढ़ने में सक्षम हो सकता है, हालांकि दुनिया भर में पढ़ने योग्य फ़ाइल * डाल सकता है, हालांकि जीमेल ऐप के संस्करणों से इनकार कर दिया गया है ऐसा करने के लिए (भले ही ऑपरेटिंग सिस्टम उन्हें चलेगा) जब तक कि कुछ /../../ टाइप ट्रिकरी के साथ इंगित न किया जाए। –

0

4 लाइन

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

के बजाय इस प्रयास करें यदि यह sdcard में है:

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

नोट करें कि पहले काम नहीं करेगा, क्योंकि प्रश्न में एप्लिकेशन को _directly_ के अंतर्गत/डेटा लिखने की अनुमति नहीं है, लेकिन केवल निर्दिष्ट सबफ़ोल्डर में/डेटा के भीतर। पढ़ने के लिए एक और ऐप की क्षमता जो भी फाइल बनाई गई है, उसके द्वारा निर्धारित अनुमति पर आधारित होगी, हालांकि _mail_ और _willingness_ पढ़ने के लिए जीमेल ऐप के मामले में काफी समान नहीं है। –

0
इसके बजाय Environment.getDataDirectory()+"/abc.txt" का उपयोग करने का

आप Environment.getExternalStorageDirectory()+"/abc.txt" उपयोग करने का प्रयास किया?

मुझे लगता है कि इस बदलाव को चाल चलनी चाहिए। बेशक, इसके लिए काम करने के लिए, आपको फोन के एमुलेटर के एसडी कार्ड पर फाइल रखना होगा।

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

यकीन है कि वहाँ है आपके डिवाइस/एम्युलेटर के sdcard में 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 से

(कुछ संशोधनों है कि आप लॉलीपॉप पर इस कोड को चलाने के लिए अनुमति देता है के साथ):

इसके अलावा अपने परियोजना की मालसूची फ़ाइल में इन दो अनुमतियाँ शामिल हैं।

मूल रूप से आप (CachedFileProvider कहते हैं, नीचे देखें) अपने स्वयं के वर्ग कि ContentProvider फैली लागू है, और (अंदर/आवेदन टैग) मेनिफेस्ट के अगले क्षेत्र जोड़ने की जरूरत:

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

जब एक मेल चयनकर्ता खोलने, CachedFileProvider के लिए

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

कोड: अगले कोड का उपयोग

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; 
    } 
} 
संबंधित मुद्दे