如何在列表视图中显示文件名和文件大小

3

在我的文件夹中,我已经给出了路径并在列表视图中显示了文件,但我希望在列表中显示文件的大小。我给你一段代码片段,通过这段代码我可以在列表视图中获取所有文件名,但我希望在列表中显示文件名和其大小,请帮助我。

String root_sd = Environment.getExternalStorageDirectory().toString();
    file = new File( root_sd + "/recycle/" ) ;       
    File list[] = file.listFiles();

    for( int i=0; i< list.length; i++)
    {
            myList.add( list[i].getName() );
    }

    setListAdapter(new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, myList ));
    setContentView(R.layout.main);

     mFilePathTextView = (TextView)findViewById(R.id.file_path_text_view);

        mStartActivityButton = (Button)findViewById(R.id.start_file_picker_button);
        mStartActivityButton.setOnClickListener(this);  

在列表项被点击时,会触发这个方法。它接收四个参数:ListView对象,当前被点击的View视图,被点击的位置position和行id。

需要注意的是,该方法是由父类的onListItemClick方法调用的。

    final File temp_file = new File( file, myList.get( position ) );   

    AlertDialog alertbox = new AlertDialog.Builder(this)
    .setTitle("File Detail")
    .setMessage("File Name:"+temp_file)
    .setPositiveButton("Delete",
            new DialogInterface.OnClickListener() {

                // do something when the button is clicked
                public void onClick(DialogInterface arg0, int arg1) {
                    DeleteRecursive(temp_file);
                    refresh();
                    //mylist.invalidateViews();
                    //adapter.notifyDataSetChanged();

                }
            })
    .setNegativeButton("Restore", new DialogInterface.OnClickListener() {

        // do something when the button is clicked
        public void onClick(DialogInterface arg0, int arg1) {

             String s5=temp_file.toString();
                String z1[]={"Field1","Field2"};


                try         
                {
                    String from= temp_file.toString();

here my xml file******************************

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <ListView
    android:id="@+id/mylist"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:background="#aa0000"
    android:drawSelectorOnTop="false" />

我想在子项中显示文件大小,请提供解决方案,修改我的xml文件和代码应该怎么做。

3个回答

9

这样做

该方法将计算文件大小

public String readableFileSize(long size) {
        if (size <= 0)
            return "0";
        final String[] units = new String[] { "B", "KB", "MB", "GB", "TB" };
        int digitGroups = (int) (Math.log10(size) / Math.log10(1024));
        return new DecimalFormat("#,##0.#").format(size / Math.pow(1024, digitGroups)) + " " + units[digitGroups];
    }

在你的代码中
for( int i=0; i< list.length; i++)
{
                myList.add( list[i].getName() + "  " + readableFileSize(list[i].length()));
}

你说得对,但我已经在列表视图上应用了点击事件,所以我想在子项中显示文件大小,请给我一个解决方案,需要更改我的XML文件和代码,因为在点击后它出现错误。 - Abhishek Maheshwari
没有明确了解你想要做什么。我认为你想把文件大小传递到下一个活动? - Biraj Zalavadia
不要,我只想在点击列表视图后执行onListItemClick(ListView l, View v, int position, long id),但出现了错误。我已经更新了上面的代码。 - Abhishek Maheshwari

3

看这个……

import java.io.File;

  public class FileSizeExample 
   {
    public static void main(String[] args)
      { 
    File file =new File("c:\\java_xml_logo.jpg");

    if(file.exists()){

        double bytes = file.length();
        double kilobytes = (bytes / 1024);
        double megabytes = (kilobytes / 1024);
        double gigabytes = (megabytes / 1024);
        double terabytes = (gigabytes / 1024);
        double petabytes = (terabytes / 1024);
        double exabytes = (petabytes / 1024);
        double zettabytes = (exabytes / 1024);
        double yottabytes = (zettabytes / 1024);

        System.out.println("bytes : " + bytes);
        System.out.println("kilobytes : " + kilobytes);
        System.out.println("megabytes : " + megabytes);
        System.out.println("gigabytes : " + gigabytes);
        System.out.println("terabytes : " + terabytes);
        System.out.println("petabytes : " + petabytes);
        System.out.println("exabytes : " + exabytes);
        System.out.println("zettabytes : " + zettabytes);
        System.out.println("yottabytes : " + yottabytes);
    }else{
         System.out.println("File does not exists!");
    }

}
  }

我想在子项中显示文件大小,请给我解决方案,告诉我需要在我的XML文件和代码中做哪些更改。 - Abhishek Maheshwari

0

在这里,计算字节数的大小

    AssetManager manager = getAssets();
    InputStream in= null;
    try {
      in= manager.open("pic.png");
      Bitmap bitmap = BitmapFactory.decodeStream(in);

      int bt = in.available(); // here is the size in no. of byte

      ImageView imgView= (ImageView) findViewById(R.id.imageView01);
      imgView.setImageBitmap(bitmap);
    } catch (IOException e) {
      e.printStackTrace();
    }

这似乎并没有回答问题。 - nobody

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接