如何使用android发送视频附件

如何使用android发送视频附件,android,image,video,android-intent,bytearray,Android,Image,Video,Android Intent,Bytearray,我在这个问题上一直在慢慢地向前推进。以下是最新情况 我有一个应用程序,允许一个用户向另一个用户发送附件 这是解剖图: 要获取要发送的附件,请使用以下代码: public void onGetAttachmentClicked(View view) { Intent attachment = new Intent(Intent.ACTION_GET_CONTENT); attachment.setType("*/*"); startActivityF

我在这个问题上一直在慢慢地向前推进。以下是最新情况

我有一个应用程序,允许一个用户向另一个用户发送附件

这是解剖图:

要获取要发送的附件,请使用以下代码:

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();
}
如果我使用apachecommon,代码在转换过程中仍然会失败,并且出现相同的错误

context.getContentResolver().openInputStream(uri);
byte[] attachmentBites = IOUtils.toByteArray(in);
注意:

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
如果附件是一个图像,那么一切都可以正常工作——代码完全相同。但是如果附件是视频,那么我会得到错误。另外,为了测试,我用手机的摄像机拍摄视频

人们如何发送视频附件

理想情况下,通过流媒体。读取文件并写入网络。它的代码与您正在使用的代码类似,但是您可以从
HttpURLConnection
或其他任何形式获得其他形式的
OutputStream

错误跟踪就是您看到的,我可以做些什么来查看跟踪中的更多错误吗


通常有第二节,列出“引起的”异常。如果你不明白,我对这种行为没有很好的解释,但是如果它不在那里,你就无法得到它,除了可能把你自己的
尝试
/
捕获
块放在
doInBackground()

中,堆栈跟踪应该比你拥有的更多。另外,请记住,除非视频非常短(例如几秒钟),否则您没有足够的堆空间将其全部加载到内存中。@commonware:wow!这是很有启发性的。我会做一些实验。与此同时,我不得不问。1) 人们如何发送视频附件?2) 错误跟踪就是你所看到的,我能做些什么来从跟踪中看到更多的错误吗?这很有趣,我只是试着用Gmail作为附件发送相同的视频文件。Gmail做得很好。它将我的附件显示为
CAM00366.mp4
。谢谢你的帮助。现在我必须去研究如何将视频从android设备传输到blobstore。