2016-12-24 16 views
1

私は現在、ギャラリーから写真を撮るアプリを作成しようとしています。その写真をメールアドレスに送信するメールに添付します。私はまた、ギャラリーから複数の写真を選択する方法を理解できません。電子メールの送信中にファイルを添付する方法は?

Here is my MainActivity 

import android.Manifest; 
import android.app.Activity; 
import android.content.Intent; 
import android.content.pm.PackageManager; 
import android.database.Cursor; 
import android.net.Uri; 
import android.os.Build; 
import android.os.Bundle; 
import android.provider.MediaStore; 
import android.support.v4.content.ContextCompat; 
import android.util.Log; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.Toast; 


public class MainActivity extends Activity implements View.OnClickListener { 

    EditText editTextEmail, editTextSubject, editTextMessage; 
    Button btnSend, btnAttachment; 
    String email, subject, message, attachmentFile; 
    Uri URI = null; 
    private static final int PICK_FROM_GALLERY = 0; 
    private static final int READ_FROM_GALLERY= 1; 
    int columnIndex; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     editTextEmail = (EditText) findViewById(R.id.editTextTo); 
     editTextSubject = (EditText) findViewById(R.id.editTextSubject); 
     editTextMessage = (EditText) findViewById(R.id.editTextMessage); 
     btnAttachment = (Button) findViewById(R.id.buttonAttachment); 
     btnSend = (Button) findViewById(R.id.buttonSend); 

     btnSend.setOnClickListener(this); 
     btnAttachment.setOnClickListener(this); 

    } 

    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     if (requestCode == PICK_FROM_GALLERY && resultCode == RESULT_OK) { 
      /** 
      * Get Path 
      */ 
      Uri selectedImage = data.getData(); 
      String[] filePathColumn = { MediaStore.Images.Media.DATA }; 
      Cursor cursor = getContentResolver().query(selectedImage, 
        filePathColumn, null, null, null); 
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){ 
       if (ContextCompat.checkSelfPermission(this, 
         Manifest.permission.READ_EXTERNAL_STORAGE)== 
         PackageManager.PERMISSION_GRANTED){ 
       } else { 
        if (shouldShowRequestPermissionRationale(Manifest.permission.READ_EXTERNAL_STORAGE)){ 
         Toast.makeText(this, 
           "Need access to open file", Toast.LENGTH_SHORT).show(); 
        } else { 
         requestPermissions(new String[] {Manifest.permission.READ_EXTERNAL_STORAGE}, 
           READ_FROM_GALLERY); 
        } 
       } 
      } else { 
       cursor.moveToFirst(); 
       columnIndex = cursor.getColumnIndex(filePathColumn[0]); 
       attachmentFile = cursor.getString(columnIndex); 
       Log.e("Attachment Path:", attachmentFile); 
       URI = Uri.parse("file://" + attachmentFile); 
       cursor.close(); 
      } 
     } 
    } 

    @Override 
    public void onClick(View v) { 

     if (v == btnAttachment) { 
      openGallery(); 

     } 
     if (v == btnSend) { 
      try { 
       email = editTextEmail.getText().toString(); 
       subject = editTextSubject.getText().toString(); 
       message = editTextMessage.getText().toString(); 

       final Intent emailIntent = new Intent(
         android.content.Intent.ACTION_SEND); 
       emailIntent.setType("plain/text"); 
       emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, 
         new String[] { email }); 
       emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, 
         subject); 
       if (URI != null) { 
        emailIntent.putExtra(Intent.EXTRA_STREAM, URI); 
       } 
       emailIntent 
         .putExtra(android.content.Intent.EXTRA_TEXT, message); 
       this.startActivity(Intent.createChooser(emailIntent, 
         "Sending email...")); 

      } catch (Throwable t) { 
       Toast.makeText(this, 
         "Request failed try again: " + t.toString(), 
         Toast.LENGTH_LONG).show(); 
      } 
     } 

    } 

    public void openGallery() { 
     Intent intent = new Intent(); 
     intent.setType("image/*"); 
     intent.setAction(Intent.ACTION_GET_CONTENT); 
     intent.putExtra("return-data", true); 
     startActivityForResult(
       Intent.createChooser(intent, "Complete action using"), 
       PICK_FROM_GALLERY); 

    } 

} 
+1

http://stackoverflow.com/questions/2264622/android-multiple-email-attachments-using-intent – sasikumar

答えて

0

emailIntent.setType( "プレーン/テキスト")。コードで

は、上のリンクを変更 -

emailIntent.setType( "画像/ PNG")。

関連する問題