0

I use basic4android and I want to know the size of selected image from gallery.

my code is :

      Dim PicChooser As ContentChooser
    PicChooser.Initialize("PicChooser")
    PicChooser.Show("image/*", "Select a pic")

Sub PicChooser_Result(Success As Boolean, Dir As String, FileName As String)
If Success = True Then
        Dim inp As InputStream
        inp = File.OpenInput(Dir, FileName)
        Dim btm As Bitmap
        btm.Initialize2(inp)
end if
end Sub

I use below method in b4a but it doesn't work.

File.Size(Dir,FileName)

it returns zero because Dir and Filename in this sub doesn't really shows the path of the file.

salih kallai
  • 879
  • 2
  • 13
  • 34
Salar Ashgi
  • 128
  • 2
  • 13

2 Answers2

1

Somewhere i found this maybe untested code:

public static String getContentSizeFromUri(Context context, Uri uri) {
    String contentSize = null;
    String[] proj = {MediaStore.Images.Media.SIZE };

    CursorLoader cursorLoader = new CursorLoader(
            context,
            uri, proj, null, null, null);

    Cursor cursor = cursorLoader.loadInBackground();

    if(cursor != null)
        {
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.SIZE);

        if (cursor.moveToFirst() )
            contentSize = cursor.getString(column_index);
        }

    return contentSize;
}

Check if return value is null before use.

greenapps
  • 11,154
  • 2
  • 16
  • 19
0

If you already get the Uri of the file, you can use the following code to get some information

            if (uri != null) {                    
                File file = new File(uri.getPath());
                JSONObject jsonObject = new JSONObject();
                try {
                    jsonObject.put("FileName", file.getName());
                    jsonObject.put("FilePath", file.getAbsolutePath());
                    jsonObject.put("FileSize", file.length());                              
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
BNK
  • 23,994
  • 8
  • 77
  • 87