2015-02-27 20 views
5

Ecco il report contengono il percorso (percorso in sdcard in formato stringa)Come convertire un file in Base64?

File dir = Environment.getExternalStorageDirectory(); 
File yourFile = new File(dir, report); 
String encodeFileToBase64Binary = encodeFileToBase64Binary(yourFile); 

private static String encodeFileToBase64Binary(File fileName) throws IOException { 
    byte[] bytes = loadFile(fileName); 
    byte[] encoded = Base64.encodeBase64(bytes); 

    String encodedString = new String(encoded); 
    return encodedString; 
} 

nel byte [] linea codificata questo errore. Il metodo encodeBase64 (byte []) non è definito per il tipo di Base64

+0

è la vostra domanda: che cosa dovrebbe essere usato al posto di 'encodeBase64' nel contesto Android? Hai provato http://developer.android.com/reference/android/util/Base64.html encode() API? – sandrstar

risposta

5
String value = Base64.encodeToString(bytes, Base64.DEFAULT); 

Ma si può direttamente convertirlo in a String Speri questo lavoro per voi.

0

Puoi provare questo.

ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); 
... 
byte[] byteArray = byteArrayOutputStream.toByteArray(); 
base64Value = Base64.encodeToString(byteArray, Base64.DEFAULT); 
2

Credo che questi 2 codici di esempio aiuteranno almeno qualcuno nello stesso modo in cui molti mi hanno aiutato attraverso questa piattaforma. Grazie a StackOverflow.

// Converting Bitmap image to Base64.encode String type 
    public String getStringImage(Bitmap bmp) { 
     ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
     bmp.compress(Bitmap.CompressFormat.JPEG, 100, baos); 
     byte[] imageBytes = baos.toByteArray(); 
     String encodedImage = Base64.encodeToString(imageBytes, Base64.DEFAULT); 
     return encodedImage; 
} 
    // 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; 
    } 

Sarò felice di rispondere a qualsiasi domanda riguardante questi codici.

0

Per convertire un file in Base64:

File imgFile = new File(filePath); 
if (imgFile.exists() && imgFile.length() > 0) { 
    Bitmap bm = BitmapFactory.decodeFile(filePath); 
    ByteArrayOutputStream bOut = new ByteArrayOutputStream(); 
    bm.compress(Bitmap.CompressFormat.JPEG, 100, bOut); 
    String base64Image = Base64.encodeToString(bOut.toByteArray(), Base64.DEFAULT); 
} 
0
//Convert Any file, image or video or txt into base64 

1.Import the below Dependancy 
compile 'commons-io:commons-io:2.4' 

2.Use below Code to convert file to base64 
File file = new File(filePath); //file Path 
byte[] b = new byte[(int) file.length()]; 
try { 
FileInputStream fileInputStream = new FileInputStream(file); 
fileInputStream.read(b); 
for (int j = 0; j < b.length; j++) { 
System.out.print((char) b[j]); 
} 
} catch (FileNotFoundException e) { 
System.out.println("File Not Found."); 
e.printStackTrace(); 
} catch (IOException e1) { 
System.out.println("Error Reading The File."); 
e1.printStackTrace(); 
    } 

byte[] byteFileArray = new byte[0]; 
    try { 
    byteFileArray = FileUtils.readFileToByteArray(file); 
    } catch (IOException e) { 
      e.printStackTrace(); 
    } 

    String base64String = ""; 
    if (byteFileArray.length > 0) { 
    base64String = android.util.Base64.encodeToString(byteFileArray, android.util.Base64.NO_WRAP); 
    Log.i("File Base64 string", "IMAGE PARSE ==>" + base64String); 
     } 
Problemi correlati