【问题标题】:how do I send video attachment with android如何使用 android 发送视频附件
【发布时间】: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


【解决方案1】:

人们如何发送视频附件?

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

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

通常有第二节,列出“Caused by”异常。如果你不明白,我对这种行为没有很好的解释,但如果它不存在,你无能为力,除了可能在你的东西周围放置你自己的 try/catch 块在doInBackground()

【讨论】:

  • 这很有趣,我刚刚尝试使用 Gmail 作为附件发送相同的视频文件。 Gmail 做得很好。它显示我的附件为CAM00366.mp4。谢谢您的帮助。现在我必须研究如何将视频从 android 设备流式传输到 blobstore。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多