【发布时间】:2014-04-18 11:36:02
【问题描述】:
我一直在努力解决这个问题。以下是最新状态。
我有一个应用程序允许一个用户向另一个用户发送附件。
这是解剖结构:
要获取要发送的附件,使用以下代码:
public void onGetAttachmentClicked(View view) {
Intent attachment = new Intent(Intent.ACTION_GET_CONTENT);
attachment.setType("*/*");
startActivityForResult(Intent.createChooser(attachment, "Attachment"),
ATTACHMENT_REQUEST_CODE);
}
….
//inside onActivityResult
if (requestCode == ATTACHMENT_REQUEST_CODE) {
mAttachmentUri = data.getData();
}
….
//inside do in background, I convert uri to byte[] which I then send to the blobstore
问题
无论我使用哪种方法将 uri 转换为字节数组,我都会得到相同的错误日志
03-13 08:15:24.753: W/dalvikvm(9451): threadid=21: thread exiting with uncaught exception (group=0x40d9a2a0)
03-13 08:15:24.824: E/AndroidRuntime(9451): FATAL EXCEPTION: AsyncTask #4
03-13 08:15:24.824: E/AndroidRuntime(9451): java.lang.RuntimeException: An error occured while executing doInBackground()
03-13 08:15:24.824: E/AndroidRuntime(9451): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
03-13 08:15:24.824: E/AndroidRuntime(9451): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
那里不多。所以我使用调试器单步执行,这就是我发现的。当我使用以下方法时,在经过一定次数的迭代后,while循环中会出现错误
public static byte[] uriToByteArray(Uri uri, Context context) throws IOException {
InputStream inputStream = context.getContentResolver().openInputStream(uri);
// this dynamically extends to take the bytes you read
ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream();
// this is storage overwritten on each iteration with bytes
int bufferSize = 1024;
byte[] buffer = new byte[bufferSize];
// we need to know how may bytes were read to write them to the byteBuffer
int len = 0;
while ((len = inputStream.read(buffer)) != -1) {
byteBuffer.write(buffer, 0, len);
}
// and then we can return your byte array.
return byteBuffer.toByteArray();
}
如果我使用apache common,代码在转换过程中仍然失败,并出现同样的错误
context.getContentResolver().openInputStream(uri);
byte[] attachmentBites = IOUtils.toByteArray(in);
注意:
如果附件是图片,则一切正常——代码完全相同。但如果附件是视频,那么我会收到错误消息。另外,为了测试,我用手机的摄像机拍摄视频。
【问题讨论】:
-
堆栈跟踪应该比您拥有的更多。另外,请记住,除非视频非常短(例如,几秒钟),否则您没有足够的堆空间将其全部加载到内存中。
-
@CommonsWare:哇!这很有启发性。我会做一些实验。与此同时,我不得不问。 1)人们如何发送视频附件? 2) 错误跟踪就是您所看到的,我可以做些什么来从跟踪中查看更多错误吗?
标签: android image video android-intent bytearray