2010-11-08 15 views
11

डिफ़ॉल्ट रूप से, आंतरिक संग्रहण में सहेजी गई फ़ाइलें आपके एप्लिकेशन के लिए निजी हैं और अन्य एप्लिकेशन उन्हें एक्सेस नहीं कर सकते हैं (न ही उपयोगकर्ता)।एंड्रॉइड - ईमेल में एक फ़ाइल संलग्न करने में सक्षम नहीं

मैं डीडीएमएस में फाइल "/ डेटा/डेटा/पैकेज_नाम/फाइल/फाइल एक्सप्लोर में फ़ाइल देख सकता हूं, लेकिन जब मैंने उपरोक्त फ़ाइल यूआरआई को ईमेल में छवि यूआरआई का उपयोग करके संलग्न किया, तो मैंने देखा कि संलग्न फाइल 0kb है। मैं एंड्रॉयड के डिफ़ॉल्ट ईमेल एपीआई का इस्तेमाल किया है।

किसी को भी मुझे बता सकते हैं, कैसे ईमेल में एक फ़ाइल संलग्न करने के लिए है कि आवेदन करने के लिए निजी है?

हालांकि मैं सफल एसडी कार्ड में फाइल को सेव करने में सक्षम हूँ और एसडी कार्ड से फाइल को संलग्न करना, यह ठीक काम कर रहा है।

लेकिन यदि एसडी कार्ड उपलब्ध नहीं है और एस फ़ाइल को आंतरिक संग्रहण में ले जा रहा है, फिर मैं उन्हें ईमेल में कैसे संलग्न कर सकता हूं।

String FILENAME = "hello_file.txt"; 
String string = "hello world!";FileOutputStream fos = openFileOutput(FILENAME,  Context.MODE_PRIVATE); 
fos.write(string.getBytes()); 
fos.close(); 

File imageFile = getFileStreamPath(FILENAME); 
Uri imageUri = Uri.fromFile(imageFile); 

final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND); 
emailIntent.setType("*/*"); 
emailIntent.putExtra(android.content.Intent.EXTRA_STREAM,imageUri); 

this.startActivityForResult(Intent.createChooser(emailIntent, "Send mail..."),SUB_ACTIVITY); 
+1

क्या आपने कभी इस समस्या के समाधान में भाग लिया था? – JehandadK

+0

मुझे इसी तरह के मुद्दे का सामना करना पड़ा। एसडी कार्ड से जो ठीक काम करता है ... लेकिन आंतरिक भंडारण से मैं इस कार्यक्षमता में सफल नहीं हुआ। शायद एमआईएमई प्रकारों के साथ खेलना मदद करेगा .. – Mike

+0

क्या आप इसे elobrate कृपया कर सकते हैं? – pankaj

उत्तर

1

जब फ़ाइल को सहेजने Context.MODE_PRIVATE के बजाय Context.MODE_WORLD_READABLE का उपयोग करें। फिर अन्य ऐप्स को फ़ाइल तक पहुंच होगी।

+0

@, मैंने पहले से ही MODE_WORLD_WRITEABLE के साथ प्रयास किया था, लेकिन यह काम नहीं करता है। – pankaj

+0

@ पंकज लेकिन क्या आपने MODE_WORLD_READABLE के साथ प्रयास किया है? –

6

जब आप आंतरिक भंडारण से फ़ाइल अटैच करने की कोशिश, जीमेल लॉग में एक त्रुटि लिखते हैं:

ERROR/Gmail(...): file:// attachment paths must point to file:///mnt/sdcard. 

ई-मेल आवेदन आप संलग्न फाइल दिखाई देंगे, भले ही वह शारीरिक रूप से नहीं किया मौजूद हैं।

एक बाहरी भंडारण के लिए के रूप में, प्रलेखन का कहना है कि:

हर Android- संगत उपकरण है, जो आप फ़ाइलों को बचाने के लिए उपयोग कर सकते हैं एक साझा "बाह्य भंडारण" का समर्थन करता है। यह एक हटाने योग्य स्टोरेज मीडिया (जैसे एक एसडी कार्ड) या एक आंतरिक (गैर हटाने योग्य) भंडारण हो सकता है।

इसका मतलब है कि आपको डिवाइस के बारे में चिंता करने की ज़रूरत नहीं है कि बाहरी भंडारण न हो। फिर भी, बाहरी भंडारण कई बार अनुपलब्ध हो सकता है। http://developer.android.com/guide/topics/data/data-storage.html#filesExternal

+0

धन्यवाद! मैं यह नहीं समझ पाया कि यह क्यों विफल रहा था और मैंने अपनी लॉगकैट विंडो पर फ़िल्टर को बदलने के बारे में नहीं सोचा था। –

1

का संदर्भ लें मैं भी इस समस्या को आंतरिक फ़ाइलों का उपयोग कर अनुभव किया है और यद्यपि मैं /data/data//files/testFileName.txt पर MODE_WORLD_READABLE साथ openFileInput इस्तेमाल किया और अतिरिक्त के साथ "/" URI.parse का इस्तेमाल किया है (देखें नीचे), ईमेल प्राप्त प्राप्त परीक्षण अभी भी वांछित लगाव की कमी है। तो खेद है लेकिन एसडी कार्ड पर बाहरी फाइलों का उपयोग करने के अलावा, कोई जवाब नहीं है - जो मेरा अगला प्रयोग है!

कोड:

File tmpFile = new File(context.getFilesDir(), mfileName); 
     Log.d(TAG, tmpFile.toString()); 
// This shows: /data/data/org.eddiem.adeveloper.flatfiletest/files/testFile.csv 
     //File tmpFile2 = new File(context.getFileStreamPath(mfileName), mfileName); 
     //Log.v(TAG, tmpFile2.toString()); 
// This also shows: /data/data/org.eddiem.adeveloper.flatfiletest/files/testFile.csv 

     //Uri uri = Uri.fromFile(new File(context.getFileStreamPath(mfileName), mfileName)); 
     Uri uri = Uri.parse("file://" + tmpFile.toString()); 
     //Uri uri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(), 
     //        mfileName)); 
     Log.d(TAG, "Uri-path is: " + uri.getPath()); // or .toString() 

     Intent i = new Intent(android.content.Intent.ACTION_SEND); 
     i.setType("text/plain"); 
     i.putExtra(Intent.EXTRA_EMAIL, new String[]{"[email protected]"}); 
     i.putExtra(Intent.EXTRA_SUBJECT, "Test Email - with Attachment"); 
     i.putExtra(Intent.EXTRA_TEXT, "This is a test Email with an Attachment."); 
     i.putExtra(Intent.EXTRA_STREAM, uri); 
     //startActivity(Intent.createChooser(i, "Select application")); 
     startActivity(Intent.createChooser(i, "Send mail")); 
4

आप एक ContentProvider उपयोग करने के लिए अन्य अनुप्रयोगों द्वारा अपनी फ़ाइल के लिए पहुँच प्रदान करने की जरूरत है एक निजी फ़ाइल साझा करने के लिए। यहां एक शानदार उदाहरण है: Android: Attaching files from internal cache to Gmail

इसके अलावा, हालांकि ट्यूटोरियल कहा गया है कि आप Android मेनिफेस्ट फ़ाइल में अपने प्रदाता की घोषणा करने की जरूरत है, यह निर्दिष्ट यह नहीं है कि यह <application> में समाहित किया जाना चाहिए, ताकि यह सुनिश्चित करें कि यह <application> </application> के भीतर है जब आप यह घोषणा करते हैं।

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission> 
3

इस संहिता लगाव के बारे में जानकारी प्राप्त करने के तुम बाहर में मदद मिल सकती एक ही मुद्दा और निम्नलिखित मेरे लिए काम किया।

पहला प्रसारण उपकरण है, जो फ़ाइल को सूचित करने के लिए भेज/रखा होगा।

उदाहरण के लिए:

sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,Uri.parse("file://"+storagePath))); 

तब अनुलग्नक के साथ मेल भेजने के लिए कोड का उपयोग करें।

Intent email = new Intent(Intent.ACTION_SEND); 
email.putExtra(Intent.EXTRA_EMAIL, "Receiver Email Address"); 
email.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
email.putExtra(Intent.EXTRA_SUBJECT, "Subject"); 
email.putExtra(Intent.EXTRA_TEXT,"Email Text"); 

//Mime type of the attachment (or) u can use sendIntent.setType("*/*") 
//email.setType("text/plain"); 
email.setType("application/YourMimeType"); 

//Full Path to the attachment 
email.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://"+storagePath)); 
try 
{ 
    startActivity(Intent.createChooser(email, "Send Message...")); 
} 
catch (android.content.ActivityNotFoundException ex) 
{ 

} 
+0

सरल और स्पष्ट –

5
Android: Attaching files from internal cache to Gmail 




package com.stephendnicholas.gmailattach; 

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 CLASS_NAME = "CachedFileProvider"; 

    // The authority is the symbolic name for the provider class 
    public static final String AUTHORITY = "com.stephendnicholas.gmailattach.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 { 

     String LOG_TAG = CLASS_NAME + " - openFile"; 

     Log.v(LOG_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 
      // E.g. 
      // 'content://com.stephendnicholas.gmailattach.provider/Test.txt' 
      // Take this and build the path to the file 
      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(LOG_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; 
    } 
} 




<provider android:name="CachedFileProvider" android:authorities="com.stephendnicholas 





public static void createCachedFile(Context context, String fileName, 
      String content) throws IOException { 

    File cacheFile = new File(context.getCacheDir() + File.separator 
       + fileName); 
    cacheFile.createNewFile(); 

    FileOutputStream fos = new FileOutputStream(cacheFile); 
    OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF8"); 
    PrintWriter pw = new PrintWriter(osw); 

    pw.println(content); 

    pw.flush(); 
    pw.close(); 
} 




public static Intent getSendEmailIntent(Context context, String email, 
      String subject, String body, String fileName) { 

    final Intent emailIntent = new Intent( 
       android.content.Intent.ACTION_SEND); 

    //Explicitly only use Gmail to send 
    emailIntent.setClassName("com.google.android.gm","com.google.android.gm.ComposeActivityGmail"); 

    emailIntent.setType("plain/text"); 

    //Add the recipients 
    emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, 
       new String[] { email }); 

    emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject); 

    emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, body); 

    //Add the attachment by specifying a reference to our custom ContentProvider 
    //and the specific file of interest 
    emailIntent.putExtra( 
      Intent.EXTRA_STREAM, 
       Uri.parse("content://" + CachedFileProvider.AUTHORITY + "/" 
         + fileName)); 

    return emailIntent; 
} 

    enter code here 
+0

मैं वास्तव में क्या जरूरत है, लेकिन स्रोत जोड़ने अच्छा होगा: http://stephendnicholas.com/archives/974। मैंने इसे थोड़ा और अधिक संरचित के रूप में भी उपयोगी पाया। – Compufreak

0

मैं का सामना करना पड़ रहा था:

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    buttonSend = (Button) findViewById(R.id.buttonSend); 

    textTo = (EditText) findViewById(R.id.editTextTo); 
    textSubject = (EditText) findViewById(R.id.editTextSubject); 
    textMessage = (EditText) findViewById(R.id.editTextMessage); 

    buttonSend.setOnClickListener(new OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      String to = textTo.getText().toString(); 
      String subject = textSubject.getText().toString(); 
      String message = textMessage.getText().toString(); 

      Intent i = new Intent(Intent.ACTION_SEND); 
      i.setType("plain/text"); 
      File data = null; 

      try { 
       Date dateVal = new Date(); 
       String filename = dateVal.toString(); 
       data = File.createTempFile("Report", ".csv"); 
       FileWriter out = (FileWriter) GenerateCsv.generateCsvFile(
               data, "Name,Data1"); 
       i.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(data)); 
       i.putExtra(Intent.EXTRA_EMAIL, new String[] { to }); 
       i.putExtra(Intent.EXTRA_SUBJECT, subject); 
       i.putExtra(Intent.EXTRA_TEXT, message); 
       startActivity(Intent.createChooser(i, "E-mail")); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 
    }); 
} 

public class GenerateCsv 
{ 
    public static FileWriter generateCsvFile(File sFileName,String fileContent) 
    { 
     FileWriter writer = null; 

     try { 
      writer = new FileWriter(sFileName); 
      writer.append(fileContent); 
      writer.flush(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } finally { 
      try { 
       writer.close(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 

     return writer; 
    } 
} 

ऊपर कोड आप अपने मैनिफ़ेस्ट फ़ाइल के लिए निम्न अनुमति जोड़ें की आवश्यकता है:

संबंधित मुद्दे