如何将来自不同路径的图像附加到电子邮件中?

时间:2018-05-04 11:23:23

标签: android android-intent android-permissions email-attachments

我使用intent发送email主题,内容和附加图片

除了一些图像没有附加外,一切正常

  

成功附加照片的文件路径如下:

content://media/external/images/media/14960
  

未成功附加照片的文件路径如下:

content://com.android.providers.media.documents/document/image%3A14745

我正在使用此代码

Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.setType("message/rfc822");
//emailIntent.setType("application/image");
emailIntent.putExtra(Intent.EXTRA_EMAIL  , new String[]{""});
emailIntent.putExtra(Intent.EXTRA_SUBJECT, mNotes.get(position).getTitle());
emailIntent.putExtra(Intent.EXTRA_TEXT   , mNotes.get(position).getContent());

ArrayList<String[]> photos = StringManipulation.imgDeserialize(mNotes.get(position).getImgUrls());
for (String[] photo : photos){
    emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(photo[0]));
}

startActivity(Intent.createChooser(emailIntent, "Send mail..."));
  

如何将所有照片附加到电子邮件中?

我通过获取CAMERAREADWRITE EXTERNAL STORAGE

的权限获得此Uris
//.... onCreate...
    //initialize the textview for starting the camera
    TextView takePhoto = (TextView) view.findViewById(R.id.tvTakeCameraPhoto);
    takePhoto.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.d(TAG, "onClick: starting camera.");
            Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(cameraIntent, Permissions.CAMERA_REQUEST_CODE);
        }
    });

    //Initialize the textview for choosing an image from memory
    TextView selectPhoto = (TextView) view.findViewById(R.id.tvChoosePhotoFromMemory);
    selectPhoto.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.d(TAG, "onClick: accessing phones memory.");
            Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
            intent.setType("image/*");
            startActivityForResult(intent, Permissions.PICK_FILE_REQUEST_CODE);
        }
    });
//.................
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    /*
    Results when taking a new image with camera
     */
    if(requestCode == Permissions.CAMERA_REQUEST_CODE && resultCode == Activity.RESULT_OK){
        Log.d(TAG, "onActivityResult: done taking a picture.");

        //get the new image bitmap
        Bitmap bitmap = (Bitmap) data.getExtras().get("data");
        Log.d(TAG, "onActivityResult: received bitmap: " + bitmap);

        // CALL THIS METHOD TO GET THE URI FROM THE BITMAP
        Uri tempUri = getImageUri(getActivity(), bitmap);

        // CALL THIS METHOD TO GET THE ACTUAL PATH
        File takenPhotoFile = new File(tempUri.toString());

        mOnPhotoReceived.getImagePath(takenPhotoFile.getPath());
        getDialog().dismiss();
    }

    /*
    Results when selecting new image from phone memory
     */
    if(requestCode == Permissions.PICK_FILE_REQUEST_CODE && resultCode == Activity.RESULT_OK){
        Log.d(TAG, "onActivityResult: done choosing image");

        Uri selectedImageUri = data.getData();
        File file = new File(selectedImageUri.toString());
        Log.d(TAG, "onActivityResult: images: " + file.getPath());


        //send the bitmap and fragment to the interface
        mOnPhotoReceived.getImagePath(file.getPath());
        getDialog().dismiss();

    }
}

public Uri getImageUri(Context inContext, Bitmap inImage) {
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
    String path = MediaStore.Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
    return Uri.parse(path);
}

然后将侦听器传递给活动。

1 个答案:

答案 0 :(得分:0)

首先,您的应用程序本身需要权限才能读取这些Uri值标识的内容。您没有说明获得这些Uri值的位置和方式,但一般情况下您只有a short time to use them。如果您没有对内容的读取权限,那么您将失去运气。

其次,在startActivity()电话前添加此行:

emailIntent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

这会将您的读取权限传递给任何应用程序处理此Intent