Created
January 29, 2017 13:52
-
-
Save kuon/7429a70e2769d6c308fada8eab0c1abe to your computer and use it in GitHub Desktop.
Upload interface for android
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Override | |
protected void onActivityResult(int requestCode, int resultCode, Intent data) { | |
super.onActivityResult(requestCode, resultCode, data); | |
try { | |
if (requestCode == SELECT_PICTURE | |
&& resultCode == RESULT_OK | |
&& null != data) { | |
Uri selectedImage = data.getData(); | |
String[] filePathColumn = {MediaStore.Images.Media.DATA}; | |
Cursor cursor = getContentResolver().query(selectedImage, | |
filePathColumn, | |
null, null, null); | |
cursor.moveToFirst(); | |
int columnIndex = cursor.getColumnIndex(filePathColumn[0]); | |
imagePath = cursor.getString(columnIndex); | |
cursor.close(); | |
byte[] bytes = getFileBytes(new File(imagePath)); | |
imageData = Base64.encodeToString(bytes, Base64.NO_WRAP); | |
webView.loadUrl("javascript:MyApp.readFile();"); | |
} else { | |
Toast.makeText(this, "Pick an image", Toast.LENGTH_LONG).show(); | |
} | |
} catch (Exception e) { | |
Toast.makeText(this, "Something Wrong", Toast.LENGTH_LONG).show(); | |
} | |
} | |
// Astonished that java cannot do that out of the box | |
// From http://stackoverflow.com/a/9431216 | |
public static byte[] getFileBytes(File file) throws IOException { | |
ByteArrayOutputStream ous = null; | |
InputStream ios = null; | |
try { | |
byte[] buffer = new byte[4096]; | |
ous = new ByteArrayOutputStream(); | |
ios = new FileInputStream(file); | |
int read = 0; | |
while ((read = ios.read(buffer)) != -1) | |
ous.write(buffer, 0, read); | |
} finally { | |
try { | |
if (ous != null) | |
ous.close(); | |
} catch (IOException e) { | |
// swallow, since not that important | |
} | |
try { | |
if (ios != null) | |
ios.close(); | |
} catch (IOException e) { | |
// swallow, since not that important | |
} | |
} | |
return ous.toByteArray(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Where you create the webview | |
webView.addJavascriptInterface(new WebAppInterface(), "Android"); | |
// And the WebAppInterface class. | |
class WebAppInterface { | |
@JavascriptInterface | |
public void opengallery() { | |
Intent intent = new Intent(Intent.ACTION_PICK); | |
intent.setType("image/*"); | |
startActivityForResult(intent, SELECT_PICTURE); | |
} | |
@JavascriptInterface | |
public String imagePath() { | |
return imagePath; | |
} | |
@JavascriptInterface | |
public String imageData() { | |
return imageData; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment