Android准备恢复能力下载经理

时间:2015-07-02 07:19:01

标签: android

以下代码是我的简单下载管理器,我正在尝试添加简历功能。感谢@Daniel Nugent分享代码。

public class MainActivity extends ActionBarActivity {
    private ImageView image;
    private Bitmap bmp;

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

        image = (ImageView) findViewById(R.id.imageView);

        new PostAsync().execute();
    }
    @Override
    protected void onResume() {
        super.onResume();
        //put on resume functionality here....
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
    class PostAsync extends AsyncTask<String, String, Integer> {

        private ProgressDialog pDialog;

        private static final String sourcepath = "http://www.planwallpaper.com/static/images/supranatural-3d-wallpaper-images.jpg";
        @Override
        protected void onPreExecute() {
            pDialog = new ProgressDialog(MainActivity.this);
            pDialog.setMessage("Attempting download...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();
        }
        @Override
        protected Integer doInBackground(String... args) {
            int fileSize = 0;
            try {
                URL url = new URL(sourcepath);
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                connection.setRequestMethod("GET");
                connection.connect();
                fileSize = connection.getContentLength();
                InputStream inputStream = connection.getInputStream();
                bmp = BitmapFactory.decodeStream(inputStream);
                inputStream.close();
                connection.disconnect();
            } catch (Exception e) {
                e.printStackTrace();
            }
            return fileSize;
        }
        protected void onPostExecute(Integer fileSize) {
            pDialog.dismiss();
            MainActivity.this.image.setImageBitmap(bmp);
            Toast.makeText(MainActivity.this, "file size: " +  fileSize, Toast.LENGTH_LONG).show();
        }
    }
}

我找不到准备下载经理的简历能力的好文件。请帮帮我

我正在尝试使用这种方式准备简历下载能力,但我得到错误,我不能这样做:

HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpGet httpGet = new HttpGet(downloadPath);
HttpResponse response = httpClient.execute(httpGet, localContext);

final int fileSize = connection.getContentLength();

File file = new File(filepath);
int downloaded = 0;
if (file.exists()) {
    downloaded = (int) file.length();
    connection.setRequestProperty("Range", "bytes="+(file.length())+"-");
}else{
    connection.setRequestProperty("Range", "bytes=" + downloaded + "-");
}

0 个答案:

没有答案