編輯:關於Android編程
在MainActivity中的代碼
package com.example.f02_sdcard02; import android.os.AsyncTask; import android.os.Bundle; import android.app.Activity; import android.app.ProgressDialog; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.util.Log; import android.view.Menu; import android.view.View; import android.widget.Button; import android.widget.ImageView; public class MainActivity extends Activity { private Button button; private ProgressDialog dialog; //創建一個對話框 private String path = "http://111.0.26.119:8080/http/hangzhou.jpg"; //圖片所在的url private ImageView imageView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); dialog = new ProgressDialog(MainActivity.this); dialog.setTitle("提示"); dialog.setMessage("load......"); button = (Button) this.findViewById(R.id.button1); imageView=(ImageView)this.findViewById(R.id.imageView1); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub new MyTask().execute(path);//啟動異步任務 } }); } public class MyTask extends AsyncTask{ private String imageName; @Override protected void onPreExecute() { // TODO Auto-generated method stub super.onPreExecute(); dialog.show(); } @Override protected byte[] doInBackground(String... arg0) { // TODO Auto-generated method stub byte []data=HttpUtiles.downloadImg(arg0[0]); //獲取圖片名稱 imageName=arg0[0].substring(arg0[0].lastIndexOf("/")+1,arg0[0].length() ); Log.i("TAG", "-------->"+data.length); return data; } @Override protected void onPostExecute(byte[] result) { // TODO Auto-generated method stub super.onPostExecute(result); Bitmap bitmap=BitmapFactory.decodeByteArray(result, 0, result.length); //顯示圖片 imageView.setImageBitmap(bitmap); FileUtiles fileUtiles=new FileUtiles(); fileUtiles.fileSave(imageName, result); dialog.dismiss(); } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
package com.example.f02_sdcard02; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import org.apache.http.HttpResponse; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.util.EntityUtils; import android.util.Log; public class HttpUtiles { public static byte[] downloadImg(String path) { byte[] data = null; HttpClient client=new DefaultHttpClient(); HttpPost httpPost=new HttpPost(path); try { HttpResponse httpResponse=client.execute(httpPost); if(httpResponse.getStatusLine().getStatusCode()==200){ data=EntityUtils.toByteArray(httpResponse.getEntity()); Log.i("TAG", "-------->"+data.length); } } catch (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ client.getConnectionManager().shutdown(); } return data; }
ContentProvider的功能和意義:主要用於對外共享數據,也就是通過ContentProvider把應用中的數據共享給其他應用訪問,其他應用可以通過Content
需求:Activity(fragment)跳轉的時候當前界面裂開,上下各自拉出手機屏幕,之後跳轉到相對應的Activity.整體效果圖如下 思路: 1,在當前Acti
以前對於這個機制理解不夠深刻,現在重新整理下思路。 一、建模 我理解的接口回調就是,我這個類實現了一個接口裡的方法doSomething,然後注冊到你這裡,然後我就去做別
啦啦啦,這是山寨UC浏覽器的下拉刷新效果的第二篇,第一篇請移步Android 自定義View UC下拉刷新效果(一)我們看圖說話:主要工作1.下拉刷新的圓形向回首頁的圓形