Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: getting file size of image before sharing it (Intent.ACTION_SEND)

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.

like image 542
java.is.for.desktop Avatar asked Jan 19 '12 18:01

java.is.for.desktop


1 Answers

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

like image 106
wrapperapps Avatar answered Oct 12 '22 20:10

wrapperapps