如何选择多个图像并将其转换为base 64?

时间:2017-10-05 02:41:14

标签: android android-image

我在应用程序中工作,我希望用户选择多个并将它们转换为base64,以便我可以将它发送到服务器。是否可以从库中选择多个图像,然后将它们转换为base64,然后将其发送到服务器

            Intent intent = new Intent();
            intent.setType("*/*");


            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
                intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
            }
            intent.setAction(Intent.ACTION_GET_CONTENT);
            startActivityForResult(Intent.createChooser(intent, "android.intent.action.SEND_MULTIPLE"), SELECT_PICTURE);



@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub

    if (requestCode == SELECT_PICTURE) {

        if (resultCode == RESULT_OK) {
            //data.getParcelableArrayExtra(name);
            //If Single image selected then it will fetch from Gallery
            filePath = data.getData();

                filePath = data.getData();
                if (null != filePath) {
                    try {
                        bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), filePath);
                      //  img.setImageBitmap(bitmap);
                        if (filePath.getScheme().equals("content")) {
                            Cursor cursor = getContentResolver().query(filePath, null, null, null, null);
                            try {
                                if (cursor != null && cursor.moveToFirst()) {
                                    file_name = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));
                                   // text.setText(file_name+",");
                                    img_name.add(file_name);
                                    img_pic.add(getStringImage(bitmap));
                                    //      Toast.makeText(this, "1." + file_name, Toast.LENGTH_SHORT).show();
                                }
                            } finally {
                                cursor.close();
                            }
                        } else {

                            String path = data.getData().getPath();
                            file_name = path.substring(path.lastIndexOf("/") + 1);
                           // text.setText(file_name);
                            img_name.add(file_name);
                            img_pic.add(getStringImage(bitmap));
                            //Toast.makeText(this, "2." + file_name, Toast.LENGTH_SHORT).show();
                        }


                    } catch (IOException e) {
                        e.printStackTrace();
                    }

1 个答案:

答案 0 :(得分:0)

当然,您可以自己管理选择和显示图像,也可以依靠Androids File Intent选择器让他们选择并返回。然后,您可以使用提供的URI来检索图像,转换和发送。

获取用户选择的图像很简单,所以我不会发布,但为了防止您不熟悉,这里有一个链接可以引导您完成。 Select multiple images from android gallery

现在转换为Base64应该是asynctask

使用以下内容:

public class Base64EncodeMediaAsyncTask extends AsyncTask<Void, Void, MediaModel> {

/*///////////////////////////////////////////////////////////////
// MEMBERS
*////////////////////////////////////////////////////////////////
private static final String TAG = Globals.SEARCH_STRING + Base64EncodeMediaAsyncTask.class.getSimpleName();
private MediaModel mMediaModelToConvert;


/*///////////////////////////////////////////////////////////////
// CONSTRUCTOR
*////////////////////////////////////////////////////////////////
public Base64EncodeMediaAsyncTask(MediaModel model){
    mContext = context;
    mMediaModelToConvert = model; //it's just a file wrapper, nothing special lol.

}


/*///////////////////////////////////////////////////////////////
// OVERRIDES
*////////////////////////////////////////////////////////////////
@Override
protected MediaModel doInBackground(Void... params) {
    try{
        InputStream inputStream = new FileInputStream(mMediaModelToConvert.getAbsoluteLocalPath());//You can get an inputStream using any IO API
        byte[] bytes;
        byte[] buffer = new byte[(int) new File(mMediaModelToConvert.getAbsoluteLocalPath()).length()];
        int bytesRead;

        ByteArrayOutputStream output = new ByteArrayOutputStream();
        while ((bytesRead = inputStream.read(buffer)) != -1) {
            output.write(buffer, 0, bytesRead);
        }

        bytes = output.toByteArray();

        mMediaModelToConvert.setBase64String(Base64.encodeToString(bytes, Base64.DEFAULT));

    }catch (Exception ex){
        A35Log.e(TAG, "Failed to get base 64 encoding for file: " + mMediaModelToConvert.getAbsoluteLocalPath());
        return null;

    }

    return mMediaModelToConvert;

}
@Override
protected void onPostExecute(MediaModel success) {
    super.onPostExecute(success);

 }

}