編輯:關於Android編程
MainActivity如下:
package cc.cn; import java.io.File; import java.io.IOException; import cc.cn.UploadImageThread.UploadImageListener; import android.app.Activity; import android.content.Context; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.os.Environment; import android.provider.MediaStore; import android.view.View; import android.widget.Button; /** * Demo描述: * 利於httpmime-4.1.3.jar實現圖片的上傳 */ public class MainActivity extends Activity { private Button mCameraButton; private File imageFile; private Context mContext; private UploadImageThread mUploadImageThread; private final int REQUEST_CAMERA = 9527; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); init(); } private void init() { mContext=this; mCameraButton = (Button) findViewById(R.id.button); mCameraButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { startCamera(); } }); } public void startCamera() { String state = Environment.getExternalStorageState(); if (state.equals(Environment.MEDIA_MOUNTED)) { String saveDir = Environment.getExternalStorageDirectory()+ "/test"; File imageDir = new File(saveDir); if (!imageDir.exists()) { imageDir.mkdir(); } imageFile = new File(saveDir, "floor1.jpg"); if (!imageFile.exists()) { try { imageFile.createNewFile(); } catch (IOException e) { e.printStackTrace(); return; } } Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(imageFile)); startActivityForResult(intent, REQUEST_CAMERA); } } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); switch (requestCode) { case REQUEST_CAMERA: if (resultCode != RESULT_OK) { return; } if (imageFile != null && imageFile.exists()) { mUploadImageThread= new UploadImageThread(imageFile); mUploadImageThread.setUploadImageListener(new UploadImageListener(){ @Override public void uploadImageFail() { System.out.println("----> 圖片上傳失敗"); } @Override public void uploadImageSuccess() { System.out.println("----> 圖片上傳成功"); }}); mUploadImageThread.start(); } break; default: break; } } }
UploadImageThread如下:
package cc.cn; import java.io.File; import java.nio.charset.Charset; import org.apache.http.HttpResponse; import org.apache.http.HttpStatus; import org.apache.http.HttpVersion; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.mime.HttpMultipartMode; import org.apache.http.entity.mime.MultipartEntity; import org.apache.http.entity.mime.content.ContentBody; import org.apache.http.entity.mime.content.FileBody; import org.apache.http.entity.mime.content.StringBody; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.params.CoreConnectionPNames; import org.apache.http.params.CoreProtocolPNames; public class UploadImageThread extends Thread { private File mImageFile; private String serverURL = "Your serverURL"; private UploadImageListener mUploadImageListener; public UploadImageThread(File imageFile) { mImageFile = imageFile; } public void setUploadImageListener(UploadImageListener uploadImageListener){ mUploadImageListener=uploadImageListener; } public interface UploadImageListener{ public void uploadImageFail(); public void uploadImageSuccess(); } @Override public void run() { HttpPost httppost = new HttpPost(serverURL); ContentBody contentBody1 = new FileBody(mImageFile, ".jpg"); HttpClient httpclient = new DefaultHttpClient(); httpclient.getParams().setParameter (CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1); httpclient.getParams().setParameter (CoreConnectionPNames.CONNECTION_TIMEOUT, 15000); httpclient.getParams().setParameter (CoreConnectionPNames.SO_TIMEOUT,20000); MultipartEntity entity = new MultipartEntity (HttpMultipartMode.BROWSER_COMPATIBLE, null, Charset.forName("UTF-8")); try { entity.addPart("devsn", new StringBody("123456")); entity.addPart("status", new StringBody("123456")); entity.addPart("upfile1", contentBody1); httppost.setEntity(entity); HttpResponse response = httpclient.execute(httppost); int status = response.getStatusLine().getStatusCode(); System.out.println("---> statusCode="+status); if (status == HttpStatus.SC_OK) { mUploadImageListener.uploadImageSuccess(); } else { mUploadImageListener.uploadImageFail(); } } catch (Exception e) { mUploadImageListener.uploadImageFail(); } finally { httpclient.getConnectionManager().shutdown(); } } }
main.xml如下:
前言:前面介紹了瀑布流的基本實現,實際上瀑布流還有一些事件需要監聽。比如點擊事件,下拉和上拉事件。這裡接著上次的 android—UI—Recyc
很長時間沒有寫博客了,懶了,感慨一下。 Activity的生命周期主要就是一張下面的圖: 下面通過代碼簡單的介紹一下,具體的一些內容看代碼的注釋: package c
有過一些面試經驗的人基本都深有體會,每次面試一般都會問到Fragment的知識,所以,今天我就單獨把Fragment拿出來與大家分享一下. 會涉及到Fragment如
Content Providers是Android中四大組件之一,用於管理應用程序訪問結構化的數據。Content Providers可以壓縮數據(They encaps