2016-03-08 59 views
0

我想獲得的docx文件如下圖所示轉換DOCX,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; 

      } 
     } 
    } 
    } 

現在的結果是:

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

文件名:Imp Values.docx

現在我該如何將這個文檔轉換爲base64?

在此先感謝。

+0

我從來沒有嘗試過,但我認爲你可以轉換您的文件爲byte [像這樣]:HTTP://stackoverflow.c om/questions/6058003 /雅緻的讀取文件到字節數組在java中,然後使用Base64.encodeToString(yourByteArray,Base64.DEFAULT)將此byte []轉換爲Base64。但我讀了一些東西,這將長大你的文件大小.... – Opiatefuchs

+1

爲我的理解,它必須像轉換成Base64圖像..... – Opiatefuchs

回答

4

試試這個,希望它會工作,我做了.pdf和.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); 
} 

對於採摘文件:

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-嘗試獲取空數組異常的長度 – coder

+0

@coder你的代碼是否給出正確的文件大小! – Nisarg

+0

是它給了我 – coder

0

上面的代碼總是返回一個NullPointerException但解決它,而不任何錯誤

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