2016-03-08 7 views
0

Je suis en train d'obtenir le fichier docx comme indiqué ci-dessousdocx Convertir, doc à base64 android

public void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 

    if (resultCode == MainActivity.RESULT_OK) { 

     if (resultCode == RESULT_OK) { 
      // Get the Uri of the selected file 
      Uri uri = data.getData(); 
      String uriString = uri.toString(); 
      File myFile = new File(uriString); 
      String path = myFile.getAbsolutePath(); 
      filepath =path; 
      String displayName = null; 

      if (uriString.startsWith("content://")) { 
       Cursor cursor = null; 
       try { 
        cursor = this.getContentResolver().query(uri, null, null, null, null); 
        if (cursor != null && cursor.moveToFirst()) { 
         displayName = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME)); 
         txtFilename.setText(displayName); 
         filename = displayName; 

        } 
       } finally { 
        cursor.close(); 
       } 
      } else if (uriString.startsWith("file://")) { 
       displayName = myFile.getName(); 
       txtFilename.setText(displayName); 
       filename = displayName; 

      } 
     } 
    } 
    } 

Maintenant, le résultat est:

Chemin: /content:/com.estrongs.files/storage/emulated/0/Download/Imp%20Values.docx

Nom du fichier: Imp Values.docx

Comment puis-je convertir ce document en base64?

Merci d'avance.

+0

Je ne l'ai jamais essayé, mais je pense que vous pouvez convertir votre doc en byte [] comme ceci: http: //stackoverflow.c om/questions/6058003/élégant-façon-de-lire-fichier-en-byte-array-in-java, puis convertir cet octet [] en Base64 avec Base64.encodeToString (yourByteArray, Base64.DEFAULT). Mais je lis quelque chose que cela va grandir Votre taille de fichier .... – Opiatefuchs

+1

pour ma compréhension, cela doit être comme la conversion d'une image en Base64 ..... – Opiatefuchs

Répondre

4

Essayez ceci, espère que ça va fonctionner, je l'ai fait pour .pdf et .doc

public static String convertFileToByteArray(File f) { 
    byte[] byteArray = null; 
    try { 
     InputStream inputStream = new FileInputStream(f); 
     ByteArrayOutputStream bos = new ByteArrayOutputStream(); 
     byte[] b = new byte[1024 * 11]; 
     int bytesRead = 0; 

     while ((bytesRead = inputStream.read(b)) != -1) { 
      bos.write(b, 0, bytesRead); 
     } 

     byteArray = bos.toByteArray(); 

     Log.e("Byte array", ">" + byteArray); 

    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    return Base64.encodeToString(byteArray, Base64.NO_WRAP); 
} 

Pour fichier picking:

private void chooseFile() { 
    try { 
     Intent intent = new Intent(Intent.ACTION_GET_CONTENT); 
     intent.addCategory(Intent.CATEGORY_OPENABLE); 
     intent.setType("*/*"); 
     try { 
      startActivityForResult(intent, LOAD_IMAGE_RESULTS); 

     } catch (ActivityNotFoundException e) { 

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

} 
+0

@ Nisarg- Tentative d'obtenir la longueur de l'exception de tableau nul – coder

+0

@coder votre code donne-t-il la bonne taille de fichier !! – Nisarg

+0

Oui, il me donne – coder

0

Le code ci-dessus renvoie toujours un NullPointerException mais résolu sans aucune erreur

// Converting File to Base64.encode String type using Method 
    public String getStringFile(File f) { 
     InputStream inputStream = null; 
     String encodedFile= "", lastVal; 
     try { 
      inputStream = new FileInputStream(f.getAbsolutePath()); 

     byte[] buffer = new byte[10240];//specify the size to allow 
     int bytesRead; 
     ByteArrayOutputStream output = new ByteArrayOutputStream(); 
     Base64OutputStream output64 = new Base64OutputStream(output, Base64.DEFAULT); 

      while ((bytesRead = inputStream.read(buffer)) != -1) { 
       output64.write(buffer, 0, bytesRead); 
      } 
     output64.close(); 
     encodedFile = output.toString(); 
     } 
     catch (FileNotFoundException e1) { 
       e1.printStackTrace(); 
      } 
      catch (IOException e) { 
       e.printStackTrace(); 
      } 
     lastVal = encodedFile; 
     return lastVal; 
    }