나는 SD 카드에서 이미지를 액세스해야합니다. 나는 그들의 길을 가졌어. 어떻게 그들을 가져 가서 String base 64로 변환 할 수 있습니까?SD 카드에서 이미지를 가져 오는 방법은 무엇입니까? 그리고 문자열로 변환하는 방법 (base64)
0
A
답변
0
다음과 같이하십시오. 그러면 원하는 파일을 읽을 수 있습니다.
File dir = Environment.getExternalStorageDirectory();
File yourFile = new File(dir, "path/to/the/file/inside/the/sdcard.ext");
시도해보십시오.
0
다음 코드를 사용할 수 있습니다.
File dir = Environment.getExternalStorageDirectory();
File yourFile = new File(dir, "path/to/the/file/inside/the/sdcard.ext");
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;
}
private static byte[] loadFile(File file) throws IOException {
InputStream is = new FileInputStream(file);
long length = file.length();
if (length > Integer.MAX_VALUE) {
// File is too large
}
byte[] bytes = new byte[(int) length];
int offset = 0;
int numRead = 0;
while (offset < bytes.length && (numRead = is.read(bytes, offset, bytes.length - offset)) >= 0) {
offset += numRead;
}
if (offset < bytes.length) {
throw new IOException("Could not completely read file " + file.getName());
}
is.close();
return bytes;
}
+1
감사합니다. 시도해 볼게 – LeandroC
감사합니다. 시도해 보겠습니다. – LeandroC