找到imageview图像的大小并在textview中显示

时间:2014-03-29 16:03:44

标签: android

我必须找到从图库中检索到的图像的大小,并将其显示在主要活动的文本视图中。 这是从图库中检索图像到图像视图的代码....

          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));

2 个答案:

答案 0 :(得分:1)

File f = new File(picturepath);
int lenghtinbytes= f.length();
int lenghtinkb=lenghtinbytes/1024

答案 1 :(得分:0)

您可以通过file.length()方法获取图像大小

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
  }

现在您可以根据需要将其转换为KB或MB。