【问题标题】:Get string data from selected image from gallery using Intent使用 Intent 从图库中的选定图像中获取字符串数据
【发布时间】:2014-11-26 08:54:28
【问题描述】:

我正在开发一个应用程序,其中一部分是从手机图库中选择图像并将其作为字符串发布到 web api。问题在于我选择的图像并将其转换为要发送的字符串。这是我的代码:

    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_media_picker);

    ((Button) findViewById(R.id.buttonSelectMedia))
            .setOnClickListener(new View.OnClickListener() {

                public void onClick(View arg0) {
                    Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
                    photoPickerIntent.setType("image/*");
                    photoPickerIntent.setAction(Intent.ACTION_GET_CONTENT);
                    startActivityForResult(photoPickerIntent, 2);
                }
            });
    }

还有我的 onActivityResult:

    protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
    super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
    if (resultCode == RESULT_OK) {
        Uri selectedImage = imageReturnedIntent.getData();
        InputStream imageStream = null;
        try {
            imageStream = getContentResolver().openInputStream(selectedImage);
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        Bitmap yourSelectedImage = BitmapFactory.decodeStream(imageStream);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        yourSelectedImage.compress(Bitmap.CompressFormat.PNG,100, baos);
        byte [] arr = baos.toByteArray();
        String stringImage = Base64.encodeToString(arr, Base64.DEFAULT);
        String fileName = imageReturnedIntent.getData().getLastPathSegment();
    }

我的内存不足

    String stringImage = Base64.encodeToString(arr, Base64.DEFAULT);

大家好。

【问题讨论】:

  • 真正的问题是什么?图片有多大?你的设备有多少内存?问题似乎很明显。您的应用占用了太多内存。

标签: java android


【解决方案1】:

我找到了通往我正在寻找的最终结果的不同路径。这是我的 OnActivityResult 的一部分:

        Uri selectedImage = imageReturnedIntent.getData();

        String[] filePathColumn = {MediaStore.Images.Media.DATA};
        Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
        cursor.moveToFirst();
        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        String filePath = cursor.getString(columnIndex);

        String extension =  filePath.substring(filePath.lastIndexOf(".")+1);
        isBusy = true;

        File file = new File(filePath);
        FileInputStream mediaStream = new FileInputStream(file);

通过这段代码,我可以做以下事情:

        mediaStream.available()

然后做我的工作。向所有人致敬,感谢您的帮助。

【讨论】:

    【解决方案2】:

    为了避免在android中处理图像时出现Out of Memory ecxeption,您可以使用BitmapFactory.Options,这里有更多信息和代码Loading Large Bitmaps Efficiently

    【讨论】:

      猜你喜欢
      • 2016-07-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-03
      • 2014-05-24
      相关资源
      最近更新 更多