2015-12-03 51 views
2

这是我的代码,我想将图像附加到电子邮件并发送。transactiontoolargeexception通过电子邮件通过意向发送图像时额外

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); 
+0

Aalap,而不是编辑我的答案编辑您的文章与最新的代码和问题,你越来越 –

+0

我更新了我的帖子在这里,错误是在提到的线上。请帮我解决它.. –

回答

4

这里:

intent.putExtra(Intent.EXTRA_STREAM, bitmap); 

线造成的问题,因为bitmap尺寸为允许的活页夹事务缓冲区的大小非常大。

在这里看到:

TransactionTooLargeException

活页夹事务缓冲器具有有限的固定大小,目前的1Mb, 其由所有交易在该过程进展共享。 因此,即使大多数单个交易 的尺寸适中,但仍有许多交易正在进行时,可能会抛出此异常。

因此,要解决这个问题,而不是通过使用Intent.putExtra图像的位图,使用图像的URL,文件路径,URI,绘制对象ID,...发送最小尺寸数据。