I successfully wrote an app, based on this blog post, but simpler, which streams an image over HTTP, using a POST request.
What I can't figure out (after searching the web and crawling inside android SDK docs) is, how do I get the size of the image before I share it?
There is only a possibility to open an input stream that I see in the exampkle and anywhere else:
ContentResolver cr = getContentResolver();
InputStream is = cr.openInputStream(uri);
The given blog post example uses getBytesFromFile(is)
, so one could get the size there, but this is not a solution. Some images are huge, while android apps are limited in heap space. I want my app to work for all thinkable sizes of images (not a problem when sharing over Wifi).
Therefor, the only option for me is to "forward" the input stream to some HTTP output stream, which I do and which works. But this approach don't give me the image size before sending it.
You can also check it this way:
Uri fileUri = resultData.getData();
Cursor cursor = getActivity().getContentResolver().query(fileUri,
null, null, null, null);
cursor.moveToFirst();
long size = cursor.getLong(cursor.getColumnIndex(OpenableColumns.SIZE));
cursor.close();
Source: https://developer.android.com/training/secure-file-sharing/retrieve-info.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With