Android:从图像ID中获取字节[
-
12-10-2019 - |
题
这是我在做的事情:
在OnActivityResult中,我意图获取数据。
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
startActivityForResult(intent, TAKE_PICTURE);
此代码获取图像ID:
data.getData().getLastPathSegment();
现在,我想使用此映像ID获取字节[] Imagedata,以便我可以将图像上传到服务器上。
我该怎么做?
解决方案
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();
}
其他提示
data.getData()将为您的图像返回您的URI。 (检查数据!= null首先!)
然后,您只需要向服务器执行HTTP帖子即可。
有关HTTP邮政编码,请参见以下参考: 使用HTTP帖子发送图像
不隶属于 StackOverflow