Android:从图像ID获取字节[]

发布于 2024-10-09 05:15:59 字数 361 浏览 0 评论 0原文

这就是我正在做的事情:

在 onActivityResult 中,我获取 Intent 中的数据。

 Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
 startActivityForResult(intent, TAKE_PICTURE);

此代码获取图像 ID:

data.getData().getLastPathSegment();

现在我想使用此图像 ID 来获取 byte[] imageData,以便我可以将图像上传到服务器上。

我该怎么办?

Here is what I am doing:

In onActivityResult, I get the data in an Intent.

 Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
 startActivityForResult(intent, TAKE_PICTURE);

This code gets the image ID:

data.getData().getLastPathSegment();

Now i want to use this image ID to get byte[] imageData so I can upload the image on a server.

How do I go about it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

遮了一弯 2024-10-16 05:15:59
Uri selectedImageUri = data.getData();

String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(selectedImageUri, projection, null, null, null);
cursor.moveToFirst();
selectedImagePath = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA));
cursor.close();

try 
{
    File myFile = new File(selectedImagePath);
    int filelenghth=(int)myFile.length();
    byte [] mybytearray  = new byte [filelenghth];
    BufferedInputStream bis1 = new BufferedInputStream(new FileInputStream(myFile));

    bis1.read(mybytearray,0,mybytearray.length);

    dos.write(mybytearray,0,mybytearray.length);   // write the array to the data output stream
                                                   // or whatever else you want to do with it
    finish();
}
catch(Exception e)
{
    e.printStackTrace();
}
Uri selectedImageUri = data.getData();

String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(selectedImageUri, projection, null, null, null);
cursor.moveToFirst();
selectedImagePath = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA));
cursor.close();

try 
{
    File myFile = new File(selectedImagePath);
    int filelenghth=(int)myFile.length();
    byte [] mybytearray  = new byte [filelenghth];
    BufferedInputStream bis1 = new BufferedInputStream(new FileInputStream(myFile));

    bis1.read(mybytearray,0,mybytearray.length);

    dos.write(mybytearray,0,mybytearray.length);   // write the array to the data output stream
                                                   // or whatever else you want to do with it
    finish();
}
catch(Exception e)
{
    e.printStackTrace();
}
伴梦长久 2024-10-16 05:15:59

data.getData() 将返回图像的 URI。 (首先检查数据!= null!)

然后您只需向服务器执行 http post 即可。

有关 http post 代码,请参阅以下关于 SO 的参考:使用 Http Post 发送图像

data.getData() will return to you a URI for your image. (Check data != null first!)

then you just need to perform a http post to your server.

See the following reference on SO for the http post code: Sending images using Http Post

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文