Good afternoon, I'm trying to send a file with an SDCard via an OutputStream . I intend to get the URI from the file name that I have, and then use an InputStream to read the URI and convert to bytes in another for sending. Currently, the code is stuck in get InputStream (as indicated below) and nothing happens.
In addition, I do not know if I am using the URI correctly to get the actual path to the file to send.
Please, any help would be greatly appreciated, because I have been stuck here for a while. Thanks.
My code is:
public void sendFile() { Log.d(TAG, "sending data"); InputStream inputStream = null; String filePath = Environment.getExternalStorageDirectory() .toString() + "/" + files.get(0); Log.d(TAG, "filepath is" + filePath); Uri uri = Uri.parse(filePath); Log.d(TAG, "obtained input stream here in Activity"); Log.d(TAG, "Uri here is" + uri); // nothing happens after here, Basically stuck!! try { inputStream = getContentResolver().openInputStream(uri); ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream(); Log.d(TAG, "obtained input stream here in Activity"); int buffersize = 1024; byte[] buffer = new byte[buffersize]; int len = 0; while ((len = inputStream.read(buffer)) != -1) { byteBuffer.write(buffer, 0, len); } AppServices.write(byteBuffer.toByteArray()); } catch (IOException e) { e.printStackTrace(); } finally { if (inputStream != null) { try { inputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } }
source share