通过额外的意图

时间:2015-12-03 06:09:58

标签: android android-layout android-fragments android-intent

这是我的代码,我想将图片附加到电子邮件并发送。

    String receiverEmail = receiver.getText().toString().trim();
    String to[] = {receiverEmail};
    Intent intent = new Intent(Intent.ACTION_SEND);
    intent.setData(Uri.parse("mailto:"));
    intent.setType("imge/jpeg");
    intent.putExtra(Intent.EXTRA_EMAIL, to);
    intent.putExtra(Intent.EXTRA_SUBJECT, "subject");
    intent.putExtra(Intent.EXTRA_TEXT, "hello wats up");
    intent.putExtra(Intent.EXTRA_STREAM, bitmap );
    startActivity(intent);

我收到的错误是

Caused by: android.os.TransactionTooLargeException: data parcel size 1331968 bytes

此问题与位图文件有关。如何减小尺寸。 帮我解决这个问题。提前谢谢。

{
oncreate method....
I have my bitmap here created with instance name bitmap which i want to send in email attachment
Uri bitmapUri = getImageUri(OutgoingEmbededImage.this, bitmap); //null pointer exception error here
String bitmapPath = getPathOfUri(bitmapUri);
end of on create method
}

//getting bitmapUri here 
 private Uri getImageUri(Context context, Bitmap myBitmap){
 ByteArrayOutputStream bytes = new ByteArrayOutputStream();
 myBitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
 String path = MediaStore.Images.Media.insertImage(context.getContentResolver(), myBitmap, "Image", null);
 return Uri.parse(path);
}

//string path here
public String getPathOfUri(Uri uri){
    Cursor cursor = getContentResolver().query(uri, null, null, null, null);
    cursor.moveToFirst();
    int index = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
    return cursor.getString(index);
}

通过评论获取上述行的错误。 如果我得到位图路径然后我可以通过下面的行我猜...如果我错了,请纠正我。并希望您能理解我在代码中提到的问题,以便您可以帮助我。

intent.putExtra(Intent.ACTION_ATTACH_DATA, bitmapPath);

1 个答案:

答案 0 :(得分:4)

下面:

intent.putExtra(Intent.EXTRA_STREAM, bitmap );
由于允许Binder事务缓冲区大小,因此bitmap大小非常大,因此

行导致问题。

见这里:

TransactionTooLargeException

  

Binder事务缓冲区的固定大小有限,目前为1Mb,   由进程中的所有正在进行的事务共享。   因此,当存在许多异常时,可以抛出此异常   即使在大多数个别交易中,交易仍在进行中   中等大小。

因此,要修复此问题而不是使用Intent.putExtra传递图像的位图,请使用图像网址,文件路径,URI,可绘制ID,... 来发送最小尺寸数据。