简体   繁体   中英

Get string data from selected image from gallery using Intent

I am developing an app and part of it is selecting image from phone gallery and post it as a string to web api. The problem is with my selection of image and turning it to string to send. Here is my code:

    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);
                }
            });
    }

and my 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();
    }

I am getting an out of memmory exc on

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

Regards to all.

为了避免在处理图像时android 中出现内存不足的情况 ,您可以使用BitmapFactory.Options ,此处提供了更多信息和代码,可有效地加载大型位图

I have found different path to the final result I am looking for. This is part of my 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);

from this code I am able to do things like:

        mediaStream.available()

and do my work. Regards to all and thanks for the help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM