2015-02-27 23 views
5

đây báo cáo chứa đường dẫn (pathname trong sdcard ở định dạng chuỗi)Làm thế nào để chuyển đổi tệp sang 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; 
} 

trong byte [] dòng mã hóa nhận được lỗi này. Phương pháp này encodeBase64 (byte []) là undefined cho các loại Base64

+0

là câu hỏi của bạn: những gì nên được sử dụng thay vì 'encodeBase64' trong bối cảnh Android? Bạn đã thử http://developer.android.com/reference/android/util/Base64.html encode() API chưa? – sandrstar

Trả lời

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

Nhưng bạn có thể trực tiếp chuyển đổi nó vào Chuỗi .Hope này sẽ làm việc cho bạn.

0

Bạn có thể thử điều này.

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

Tôi tin rằng 2 mã mẫu này sẽ giúp ít nhất một người theo cùng một cách mà nhiều người đã giúp tôi thông qua nền tảng này. Nhờ 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; 
    } 

Tôi rất sẵn lòng trả lời bất kỳ câu hỏi nào liên quan đến các mã này.

0

Để chuyển đổi một tập tin để 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); 
     } 
Các vấn đề liên quan