資源簡介
Android設置頭像小案例,可以拍照也可以從本地選擇,支持大圖的裁剪,效果清晰,很不錯哦!借鑒別人的例子:http://06peng.com/archives/192

代碼片段和文件信息
package?com.kira.photocrop;
import?java.io.File;
import?java.io.FileNotFoundException;
import?com.kira.photocrop.R;
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.util.Log;
import?android.view.View;
import?android.widget.Button;
import?android.widget.ImageView;
public?class?MainActivity?extends?Activity?{
private?ImageView?imageView;
private?Button?clickBtnLocal;
private?Button?clickBtnCamera;
private?Uri?photoUri;
private?final?int?PIC_FROM_CAMERA?=?1;
private?final?int?PIC_FROM_LOCALPHOTO?=?0;
@Override
protected?void?onCreate(Bundle?savedInstanceState)?{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView?=?(ImageView)?findViewById(R.id.image);
clickBtnLocal?=?(Button)?findViewById(R.id.click_local);
clickBtnCamera?=?(Button)?findViewById(R.id.click_camera);
//本地選擇
clickBtnLocal.setonclickListener(new?View.onclickListener()?
{
@Override
public?void?onclick(View?v)?
{
doHandlerPhoto(PIC_FROM_LOCALPHOTO);//?從相冊中去獲取
}
});
//拍照
clickBtnCamera.setonclickListener(new?View.onclickListener()
{
@Override
public?void?onclick(View?v)?
{
doHandlerPhoto(PIC_FROM_CAMERA);//?用戶點擊了從照相機獲取
}
});
}
/**
?*?根據不同方式選擇圖片設置ImageView
?*?@param?type?0-本地相冊選擇,非0為拍照
?*/
private?void?doHandlerPhoto(int?type)
{
try
{
//保存裁剪后的圖片文件
File?pictureFileDir?=?new?File(Environment.getExternalStorageDirectory()?“/upload“);
if?(!pictureFileDir.exists())?{
pictureFileDir.mkdirs();
}
File?picFile?=?new?File(pictureFileDir?“upload.jpeg“);
if?(!picFile.exists())?{
picFile.createNewFile();
}
photoUri?=?Uri.fromFile(picFile);
if?(type==PIC_FROM_LOCALPHOTO)
{
Intent?intent?=?getCropImageIntent();
startActivityForResult(intent?PIC_FROM_LOCALPHOTO);
}else
{
Intent?cameraIntent?=?new?Intent(MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT?photoUri);
startActivityForResult(cameraIntent?PIC_FROM_CAMERA);
}
}?catch?(Exception?e)
{
Log.i(“HandlerPicError“?“處理圖片出現錯誤“);
}
}
/**
?*?調用圖片剪輯程序
?*/
public?Intent?getCropImageIntent()?{
Intent?intent?=?new?Intent(Intent.ACTION_GET_CONTENT?null);
intent.setType(“image/*“);
setIntentParams(intent);
return?intent;
}
/**
?*?啟動裁剪
?*/
private?void?cropImageUriByTakePhoto()?{
Intent?intent?=?new?Intent(“com.android.camera.action.CROP“);
intent.setDataAndType(photoUri?“image/*“);
setIntentParams(intent);
startActivityForResult(intent?PIC_FROM_LOCALPHOTO);
}
/**
?*?設置公用參數
?*/
private?void?setIntentParams(Intent?intent)
{
intent.putExtra(“crop“?“true“);
intent
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件????????475??2014-06-11?17:40??HeadImageSet\.classpath
?????文件????????848??2014-06-11?17:30??HeadImageSet\.project
?????文件????????953??2014-06-11?17:35??HeadImageSet\AndroidManifest.xm
?????文件??????55026??2012-12-25?15:12??HeadImageSet\ic_launcher-web.png
?????文件?????385685??2012-12-25?15:12??HeadImageSet\libs\android-support-v4.jar
?????文件????????781??2012-12-25?15:12??HeadImageSet\proguard-project.txt
?????文件????????563??2014-06-11?17:40??HeadImageSet\project.properties
?????文件???????7721??2012-12-25?15:12??HeadImageSet\res\drawable-hdpi\ic_launcher.png
?????文件???????2434??2012-12-25?15:12??HeadImageSet\res\drawable-ldpi\ic_launcher.png
?????文件???????3859??2012-12-25?15:12??HeadImageSet\res\drawable-mdpi\ic_launcher.png
?????文件??????12619??2012-12-25?15:12??HeadImageSet\res\drawable-xhdpi\ic_launcher.png
?????文件???????1237??2014-06-11?16:53??HeadImageSet\res\layout\activity_main.xm
?????文件????????257??2012-12-25?15:12??HeadImageSet\res\menu\activity_main.xm
?????文件????????344??2014-06-11?16:52??HeadImageSet\res\values\strings.xm
?????文件????????695??2012-12-25?15:12??HeadImageSet\res\values\st
?????文件????????332??2012-12-25?15:12??HeadImageSet\res\values-v11\st
?????文件????????389??2012-12-25?15:12??HeadImageSet\res\values-v14\st
?????文件???????4419??2014-06-11?17:38??HeadImageSet\src\com\kira\photocrop\MainActivity.java
?????目錄??????????0??2014-06-11?17:41??HeadImageSet\src\com\kira\photocrop
?????目錄??????????0??2014-06-11?17:41??HeadImageSet\src\com\kira
?????目錄??????????0??2014-06-11?17:41??HeadImageSet\res\drawable-hdpi
?????目錄??????????0??2014-06-11?17:41??HeadImageSet\res\drawable-ldpi
?????目錄??????????0??2014-06-11?17:41??HeadImageSet\res\drawable-mdpi
?????目錄??????????0??2014-06-11?17:41??HeadImageSet\res\drawable-xhdpi
?????目錄??????????0??2014-06-11?17:41??HeadImageSet\res\layout
?????目錄??????????0??2014-06-11?17:41??HeadImageSet\res\menu
?????目錄??????????0??2014-06-11?17:41??HeadImageSet\res\values
?????目錄??????????0??2014-06-11?17:41??HeadImageSet\res\values-v11
?????目錄??????????0??2014-06-11?17:41??HeadImageSet\res\values-v14
?????目錄??????????0??2014-06-11?17:41??HeadImageSet\src\com
............此處省略8個文件信息
- 上一篇:Curator的JAR包
- 下一篇:Java實現與FTP服務器的數據傳輸2
評論
共有 條評論