文件扩展名计数器应用程序ANDROID

时间:2015-01-25 22:01:29

标签: java android

我能够创建一个程序来从手机的内存中获取所有文件并将其显示在文本字段中。

我现在要做的是剥离所有文件名并计算类似文件扩展名的数量。我尝试使用地图,但我的程序崩溃了。请帮忙!我相信这是来自我的字符串中的空值,我尝试在eclipse中使用调试来查看为什么这没有将正确的值放入地图字段但是无法看清楚它。

由于

这是我的代码:

package com.sdfiletypes;

import java.io.File;
import java.io.FilenameFilter;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.os.StatFs;
import android.provider.MediaStore.Files;
import android.util.Log;
import android.widget.TextView;

public class MainActivity extends Activity {

int myNumber = 0;

String myString;
private TextView myFileTypes;
    //This is where I put the location of the directory I want it to find
    //I would like this to be recursive but not sure how to change it at this point
File myFile = new File("/storage/extSdCard/my_pix");
File[] pictures = myFile.listFiles();
String[] stringArray;
String[] newArray;
Map<String, Integer> myMap;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    myFileTypes = (TextView) this.findViewById(R.id.textFileType);

    String path = Environment.getExternalStorageDirectory().toString();     

    stringArray = new String[pictures.length];

    for (int i = 0; i < pictures.length; i++)
    {
        stringArray[i] = pictures[i].getName().split("\\.")[1];

        if(myMap.containsKey(stringArray[i]))
        {
            myMap.put(stringArray[i], +1);

        }
        else
            myMap.put(stringArray[i], 1);
    }

    for(Map.Entry<String, Integer> entry: myMap.entrySet())
    {
        String key = entry.getKey();
        Integer value = entry.getValue();

        myFileTypes.append(key + " = " + value.toString() + "\n");
    }

}

}

1 个答案:

答案 0 :(得分:0)

我能够使用以下代码使用List vice a Map来实现此功能:

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.widget.TextView;

public class MainActivity extends Activity {

String myString;
private TextView myFileTypes;
File myFile = new File("/storage/extSdCard/my_pix");
File[] pictures = myFile.listFiles();
String[] stringArray;
boolean flag = false;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    myFileTypes = (TextView) this.findViewById(R.id.textFileType);

    String path = Environment.getExternalStorageDirectory().toString();

    stringArray = new String[pictures.length];
    List<NameValuePair> myValuePair = new ArrayList<NameValuePair>();

    for (int i = 0; i < pictures.length; i++) {
        stringArray[i] = pictures[i].getName().split("\\.")[1];
        int j;
        for (j = 0; j < myValuePair.size(); j++) {
            if (myValuePair.get(j).getName().equals(stringArray[i])) {
                int addOne = Integer
                        .parseInt(myValuePair.get(j).getValue()) + 1;
                myValuePair.set(j, new BasicNameValuePair(stringArray[i],
                        String.valueOf(addOne)));
                break;
            }
        }

        if (j == myValuePair.size()) {
            myValuePair.add(new BasicNameValuePair(stringArray[i], String
                    .valueOf(1)));

        }

    }

    for (NameValuePair nameValuePair : myValuePair) {

        myFileTypes.append(nameValuePair.getName() + " - "
                + nameValuePair.getValue()) + "\n";
    }

}

}