0

i have to find the size of an image retrieved from gallery and display it in text view of main activity. here is the code of retrieving the image from gallery into image view....

          gallery_button.setOnClickListener(new OnClickListener() {

             @Override
               public void onClick(View v) {
                // TODO Auto-generated method stub
                 Intent i=new                                                      Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                 startActivityForResult(i,RESULT_LOADIMAGE);

                }
               });





         protected void onActivityResult(int requestCode, int resultCode, Intent data)
   {

       super.onActivityResult(requestCode, resultCode, data);


       if(requestCode==RESULT_LOADIMAGE&&resultCode==RESULT_OK&&null!=data)

        Uri selectedImages=data.getData();

        String[] filePathColon={MediaStore.Images.Media.DATA};
        Cursor cursr=getContentResolver().query(selectedImages, filePathColon, null, null, null);
        cursr.moveToFirst();
        int columnindex=cursr.getColumnIndex(filePathColon[0]);
        String picturepath=cursr.getString(columnindex);
        cursr.close();
         img.setImageBitmap(BitmapFactory.decodeFile(picturepath));
  • See this answer[http://stackoverflow.com/a/17219870/2649012](http://stackoverflow.com/a/17219870/2649012) – Phantômaxx Mar 29 '14 at 16:07
  • possible duplicate of [How to get file size in KB and MB in android](http://stackoverflow.com/questions/17219802/how-to-get-file-size-in-kb-and-mb-in-android) – Phantômaxx Mar 29 '14 at 16:07

2 Answers2

1
File f = new File(picturepath);
int lenghtinbytes= f.length();
int lenghtinkb=lenghtinbytes/1024
Lisa Anne
  • 4,482
  • 17
  • 83
  • 157
0

You can get Image size by file.length() method

protected void onActivityResult(int requestCode, int resultCode, Intent data)  {
   super.onActivityResult(requestCode, resultCode, data);
   if(requestCode==RESULT_LOADIMAGE&&resultCode==RESULT_OK&&null!=data)
    Uri selectedImages=data.getData();
    String[] filePathColon={MediaStore.Images.Media.DATA};
    Cursor cursr=getContentResolver().query(selectedImages, filePathColon, null, null, null);
    cursr.moveToFirst();
    int columnindex=cursr.getColumnIndex(filePathColon[0]);
    String picturepath=cursr.getString(columnindex);
    cursr.close();
    long size = new File(picturepath).length();//where size in bytes
  }

Now you can convert it on KB or MB as you want.