2015-04-06 49 views
0
((HomeActivity) getActivity()).contactus 
       .setOnClickListener(new OnClickListener() { 

        @Override 
        public void onClick(View arg0) { 
         // TODO Auto-generated method stub 
         sendEmail(); 
        } 
       }); 

     ((HomeActivity) getActivity()).attachmentimageview 
       .setOnClickListener(new OnClickListener() { 

        @Override 
        public void onClick(View arg0) { 
         Intent intent = new Intent(
           Intent.ACTION_PICK, 
           android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 

         intent.setType("image/*"); 
         intent.setAction(Intent.ACTION_GET_CONTENT); 

         startActivityForResult(
           Intent.createChooser(intent, "Complete action using"), 
           MY_INTENT_CLICK); 
        } 
       }); 

     return view; 
    } 

    @Override 
    public void onActivityResult(int requestCode, int resultCode, Intent data) { 
     if (resultCode == Activity.RESULT_OK) { 
      if (requestCode == MY_INTENT_CLICK) { 
       if (null == data) 
        return; 

       String selectedImagePath; 
       Uri selectedImageUri = data.getData(); 

       // MEDIA GALLERY 
       selectedImagePath = ImageFilePath.getPath(
         getActivity(), selectedImageUri); 
       Log.i("Image File Path", "" + selectedImagePath); 
//    txta.setText("File Path : \n" + selectedImagePath); 
      } 
     } 
    } 

    private void sendEmail() { 
     try { 


      Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND); 
      String[] recipients = new String[] { "Enter email" }; 
      emailIntent 
        .putExtra(android.content.Intent.EXTRA_EMAIL, recipients); 
      emailIntent 
        .putExtra(
          Intent.EXTRA_EMAIL, 
          new String[] { "[email protected],[email protected]" }); 
      emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, 
        "Feedback"); 

      emailIntent.putExtra(Intent.EXTRA_STREAM, selectedImagePath); 

      emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, 
        Html.fromHtml("")); 
      emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, "HI" 
        + "\n\n" + contactustext.getText().toString()); 
      emailIntent.setType("message/rfc822"); 
      startActivity(emailIntent); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

這是我的代碼,我想我使用給定的代碼,我能夠從廚房拿到路徑從SD卡或畫廊附加文件附加在Android的文件但是,當我在與我們聯繫按鈕則點擊如果我們不使用附件,那麼它與文本正常工作相同的工作,以獲得文件目錄,請檢查哪裏做錯了,如何解決它,請建議我actully我想發送一些文本,並附件發送通過Gmail時,當我點擊按鈕聯繫我們它重定向到附件和文本到Gmail,然後我們可以發送它。如何使用Gmail

回答

0

首先,在您的ActivityFragment之外的onCreate

public static void getVcardString() { 

String path = null;  
String vfile = null; 

vfile = "Contacts.vcf";   
Cursor phones = mContext.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, 
       null, null, null); 

phones.moveToFirst(); 
Log.i("Number of contacts", "cursorCount" +phones.getCount()); 
for(int i =0;i<phones.getCount();i++) {  

    String lookupKey = phones.getString(phones.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY)); 
    Log.i("lookupKey", " " +lookupKey); 
    Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_VCARD_URI, lookupKey); 
    AssetFileDescriptor fd; 

    try { 
     fd = mContext.getContentResolver().openAssetFileDescriptor(uri, "r"); 
     FileInputStream fis = fd.createInputStream(); 
     byte[] buf = new byte[(int) fd.getDeclaredLength()]; 
     fis.read(buf); 
     String VCard = new String(buf);   

     path = Environment.getExternalStorageDirectory().toString() + File.separator + vfile; 
     FileOutputStream mFileOutputStream = new FileOutputStream(path, true); 
     mFileOutputStream.write(VCard.toString().getBytes());  

     phones.moveToNext();    

     filevcf = new File(path); 
     Log.i("file", "file" +filevcf); 

    }catch(Exception e1) { 
     e1.printStackTrace(); 
    } 
}  
Log.i("TAG", "No Contacts in Your Phone");   
} 

創建這個方法並調用它裏面onCreate,如:再

public void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.main);  
getVcardString();   
}  

而現在,創建一個新的方法外送EmailonCreate like:

protected void data() {    
File filelocation = filevcf ;  
Intent sharingIntent = new Intent(Intent.ACTION_SEND); 
sharingIntent.setType("vnd.android.cursor.dir/email");  
sharingIntent.setType("application/x-vcard");  
sharingIntent.putExtra(Intent.EXTRA_EMAIL, "[email protected]");   
sharingIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://"+filelocation.getAbsolutePath())); 
startActivity(Intent.createChooser(sharingIntent, "Send email"));    
} 

並稱之爲data()您發送電子郵件按鈕的方法onClick像:

data(); 

請告知,如果你現在得到的任何問題,我知道。

+0

什麼xmlFilename名字? – user1340963 2015-04-06 09:02:01

+0

文件的名稱。 – 2015-04-06 09:09:14

+0

請檢查我的代碼我正在做同樣的事情但問題來了附件按鈕和發送按鈕都表現得像這一樣請檢查並編輯我的代碼 – user1340963 2015-04-06 09:13:20

0

,您可以附加文件:

Intent sharingIntent = new Intent(Intent.ACTION_SEND); 
sharingIntent.setType("vnd.android.cursor.dir/email");  
sharingIntent.putExtra(Intent.EXTRA_SUBJECT, "Please find attachment"); 
sharingIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://"+"you file's path")); 
startActivity(Intent.createChooser(sharingIntent, "Attach using...")); 
+0

請檢查我的代碼我正在做同樣的事情但問題來了附件上的按鈕和發送按鈕都像同樣請檢查和編輯我的代碼 – user1340963 2015-04-06 09:04:45

+0

看我的代碼contactus和附件按鈕,我們有不同的事情,但它表現一樣,請告訴我如何解決它 – user1340963 2015-04-06 09:09:00