获取文件大小在共享首选项中疼痛

时间:2014-04-27 11:51:32

标签: android filesize

有人可以帮助我获取SDcard文件大小并以共享首选项存储: 我无法弄清楚如何读取URL_FileSize并与已将其存储在共享首选项中的内容进行比较。如果它的URL文件更大,那么如果不忽略则存储文件大小下载..

编辑:

public class MainActivity extends Activity {


public static final String PREFS_UPDATE = "sysupdate";

public static final String external_sd = Environment
        .getExternalStorageDirectory().getAbsolutePath();
public static final File sdCard = Environment.getExternalStorageDirectory();
public static final String sdcardBaseDir = sdCard.getAbsolutePath();
public static final String externalPath = "/Android/data/com.example/";
public static final String csvFileName = "rsconfig.csv";

public static int file_size;

public static final String url = "http://www.example.com/appconfig.csv";

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    SharedPreferences prefs = getSharedPreferences(PREFS_UPDATE, 0);
    file_size = prefs.getInt("fileSize", 0);

    try {
        URL link = new URL(url);
        URLConnection urlConnection = link.openConnection();
        urlConnection.connect();
        int link_file_size = urlConnection.getContentLength();
        if (file_size < link_file_size) {
            // Download it

            File filenew = new File(sdcardBaseDir + externalPath
                    + csvFileName);
            int file_size = Integer.parseInt(String.valueOf(filenew
                    .length()));
            SharedPreferences.Editor editor = getPreferences(0).edit();
            editor.putLong("fileSize", file_size);
            editor.commit();

            startDownload();
        }
    }

    catch (Exception e) {
        e.printStackTrace();
    }

}

private void startDownload() {
    // TODO Auto-generated method stub

}

提前感谢任何建议和意见......

1 个答案:

答案 0 :(得分:0)

您必须获取url处可用文件的文件大小。为此,您可能需要在代码中执行类似的操作。

URL link = new URL(url);
URLConnection urlConnection = link.openConnection();
urlConnection.connect();
int link_file_size = urlConnection.getContentLength();
if (file_size < link_file_size){
      //Download it
}
else{
    //Ignore the file
    }