【问题标题】:Upload pdf or txt in app android and convert to base64在app android中上传pdf或txt并转换为base64
【发布时间】:2017-06-11 12:32:33
【问题描述】:

我正在为一个应用程序工作,我需要上传一个文件(来自相机或画廊的图像,来自文档的 pdf 或 txt),在 base64 中转换并将带有 base64 的字符串添加到数组中。我解决了来自相机和画廊的图像 - 现在我需要对文档做同样的事情。如何将文档从我的设备上传到我的应用程序并将其转换为 base64?这是我的图片代码:

private void selectImage(){

    final CharSequence[] item={"Camera","Document","Cancel"};

    AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);
    builder.setTitle("Carica");
    builder.setItems(item, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int i) {
            if(item[i].equals("Camera")){
                Intent intent= new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                startActivityForResult(intent,Request_camera);
            }
            else if(item[i].equals("Document")){

                Intent intent3 = new Intent();
                intent3.setType("*/*");
                intent3.setAction(Intent.ACTION_GET_CONTENT);
                startActivityForResult(Intent.createChooser(intent3,"Choose File to Upload.."),PICK_FILE_REQUEST);
                Uri.parse(Environment.getExternalStorageDirectory().toString());


            }

            else if(item[i].equals("Cancel")){
                dialog.dismiss();
            }
        }
    }).show();
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
    super.onActivityResult(requestCode,resultCode,data);

    if(resultCode== Activity.RESULT_OK){
        if(requestCode==Request_camera && button1==1){

            Bundle bundle=data.getExtras();
            final Bitmap bpm= (Bitmap)bundle.get("data");
            uploadImage.setImageBitmap(bpm);
            bitmapToBase64(bpm);

            String mynewString=bitmapToBase64(bpm);
            mydocument.set(0,mynewString);
            decode64.setText(mynewString);
            getFileExt(uploadImage.toString());

        }
         else if(requestCode==Request_camera && button1==2){
            Bundle bundle=data.getExtras();
            final Bitmap bpm= (Bitmap)bundle.get("data");
            imageView2.setImageBitmap(bpm);
            bitmapToBase64(bpm);

            String mynewString=bitmapToBase64(bpm);
            decodee2.setText(mynewString);
            decodee2.setTextColor(Color.GREEN);
        }

        else if(requestCode==Request_file){
            Uri selectImageUri=data.getData();
            uploadImage.setImageURI(selectImageUri);
            String newNome=getFileName(selectImageUri);
            decode64.setText(newNome);

            String realPath=selectImageUri.toString();

            decodee2.setText(realPath);

            File file=new File(newNome);

            getStringFile(file);
            String myDecode=getStringFile(file);
            mydocument.set(0,myDecode);


        }
    }

}


private String bitmapToBase64(Bitmap bitmap) {
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
    byte[] byteArray = byteArrayOutputStream .toByteArray();
    return Base64.encodeToString(byteArray, Base64.DEFAULT);
}

谢谢!!

【问题讨论】:

    标签: android file-upload base64


    【解决方案1】:

    将您的图像转换为 Base64;

    private  String imageToString(Bitmap bitmap){
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream);
        byte[] imageByte = byteArrayOutputStream.toByteArray();
        return Base64.encodeToString(imageByte, Base64.DEFAULT);
    }
    

    您需要先将其转换为位图。

    【讨论】:

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