【问题标题】:Android : Getting byte[] from Image IDAndroid:从图像 ID 获取字节 []
【发布时间】:2010-12-22 09:31:12
【问题描述】:

这是我正在做的事情:

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

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

此代码获取图像 ID:

data.getData().getLastPathSegment();

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

我该怎么做?

【问题讨论】:

    标签: android android-camera android-camera-intent


    【解决方案1】:
    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();
    }
    

    【讨论】:

    • 谢谢阿布舍克。这口井对我帮助很大。
    【解决方案2】:

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

    那么你只需要向你的服务器执行一个 http post。

    请参阅以下关于 SO 的参考以获取 http 邮政编码:Sending images using Http Post

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-01-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多