歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android實現拍照並上傳

Android實現拍照並上傳

日期:2017/3/1 9:59:44   编辑:Linux編程

最近看了幾篇關於Android照相機的一些文章,現在總結如下,直接上源代碼把,該說的都用注釋說完了

1.java代碼

package org.android.test;

import java.io.File;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

public class Android_mytestActivity extends Activity {
/** Called when the activity is first created. */

// 定義一個button打開照相機,定義一個imageview顯示照相機所拍攝的相片;
Button but,upload_image;
ImageView img;
// 獲取sd卡根目錄地址,並創建圖片父目錄文件對象和文件的對象;
String file_str = Environment.getExternalStorageDirectory().getPath();
File mars_file = new File(file_str + "/my_camera");
File file_go = new File(file_str + "/my_camera/file.jpg");

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.main);
but = (Button) findViewById(R.id.my_camare_button);
upload_image=(Button)findViewById(R.id.upload_image);
img = (ImageView) findViewById(R.id.my_img_view);
//拍照
but.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub

// 驗證sd卡是否正確安裝:
if (Environment.MEDIA_MOUNTED.equals(Environment
.getExternalStorageState())) {
// 先創建父目錄,如果新創建一個文件的時候,父目錄沒有存在,那麼必須先創建父目錄,再新建文件。
if (!mars_file.exists()) {
mars_file.mkdirs();
}

/*//常規情況下,我們這裡會 創建子目錄,但在這裡不用系統拍照完畢後會根據所給的圖片路徑自動去實現;
if(!file_go.exists())
{
try {
file_go.createNewFile();
} catch (IOException e) {
}}
*/
// 設置跳轉的系統拍照的activity為:MediaStore.ACTION_IMAGE_CAPTURE ;
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// 並設置拍照的存在方式為外部存儲和存儲的路徑;

intent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(file_go));
//跳轉到拍照界面;
startActivityForResult(intent, 0x1);
} else {
Toast.makeText(Android_mytestActivity.this, "請先安裝好sd卡",
Toast.LENGTH_LONG).show();
}
}
});
//上傳
upload_image.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(file_go.exists())
{
//驗證圖片存在後就實現,上傳功能,得到與服務器的輸出流...
//什麼URLconnection ,HttpURLconnectio等都可以.......
Toast.makeText(Android_mytestActivity.this, "上傳中....",
Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(Android_mytestActivity.this, "請先拍照....",
Toast.LENGTH_LONG).show();
}
}
});

}

//拍照結束後顯示圖片;
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
// 判斷請求碼和結果碼是否正確,如果正確的話就在activity上顯示剛剛所拍照的圖片;
if (requestCode == 0x1 && resultCode == this.RESULT_OK) {
/* 使用BitmapFactory.Options類防止OOM(Out Of Memory)的問題;
創建一個BitmapFactory.Options類用來處理bitmap;*/
BitmapFactory.Options myoptions=new BitmapFactory.Options();
/* 設置Options對象inJustDecodeBounds的屬性為true,用於在BitmapFactory的
decodeFile(String path, Options opt)後獲取圖片的高和寬;
而且設置了他的屬性值為true後使用BitmapFactory的decodeFile()方法無法返回一張
圖片的bitmap對象,僅僅是把圖片的高和寬信息給Options對象;
*/
myoptions.inJustDecodeBounds=true;
BitmapFactory.decodeFile(file_go.getAbsolutePath(),myoptions);
//根據在圖片的寬和高,得到圖片在不變形的情況指定大小下的縮略圖,設置寬為222;
int height=myoptions.outHeight*222/myoptions.outWidth;
myoptions.outWidth=222;
myoptions.outHeight=height;
//在重新設置玩圖片顯示的高和寬後記住要修改,Options對象inJustDecodeBounds的屬性為false;
//不然無法顯示圖片;
myoptions.inJustDecodeBounds=false;
//還沒完這裡才剛開始,要節約內存還需要幾個屬性,下面是最關鍵的一個;
myoptions.inSampleSize=myoptions.outWidth/222;
//還可以設置其他幾個屬性用於縮小內存;
myoptions.inPurgeable=true;
myoptions.inInputShareable=true;
myoptions.inPreferredConfig=Bitmap.Config.ARGB_4444;// 默認是Bitmap.Config.ARGB_8888
//成功了,下面就顯示圖片咯;
Bitmap bitmat = BitmapFactory.decodeFile(file_go.getAbsolutePath(),myoptions);
img.setImageBitmap(bitmat);

} else {
System.out.println("不顯示圖片");
}
super.onActivityResult(requestCode, resultCode, data);
}
}

Copyright © Linux教程網 All Rights Reserved