Android Studio交換画像:写真または写真関連コードの選択
4812 ワード
ポップアップメッセージボックスコード
アルバムから写真コードを選択:
カメラから取得
//メモリーカードが使えるかどうかを判断する
//戻り値で写真を設定する
//クリップ画像ソース:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(" ");
builder.setMessage(" !");
builder.setPositiveButton(" ", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
tempFile = new File(Environment.getExternalStorageDirectory(), PHOTO_FILE_NAME);
gallery();
}
});
builder.setNegativeButton(" ", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
tempFile = new File(Environment.getExternalStorageDirectory(), PHOTO_FILE_NAME);
camera();
}
});
builder.create();//
builder.show();//
アルバムから写真コードを選択:
/**
*
*/
public void gallery() {
Intent intent=new Intent(Intent.ACTION_GET_CONTENT);//ACTION_OPEN_DOCUMENT
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("image/jpeg");
if(android.os.Build.VERSION.SDK_INT>=android.os.Build.VERSION_CODES.KITKAT){
startActivityForResult(intent, PHOTO_REQUEST_GALLERY);
}else{
startActivityForResult(intent, PHOTO_REQUEST_GALLERY2);
}
}
カメラから取得
/**
*
*/
public void camera() {
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
// ,
if (hasSdcard()) {
intent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(new File(Environment.getExternalStorageDirectory(), PHOTO_FILE_NAME)));
}
startActivityForResult(intent, PHOTO_REQUEST_CAMERA);
}
//メモリーカードが使えるかどうかを判断する
/**
* SD
*
* @return
*/
private boolean hasSdcard() {
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
return true;
} else {
return false;
}
}
//戻り値で写真を設定する
/**
*
*/
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PHOTO_REQUEST_GALLERY2) {
if (data != null) {
//
String path = GetPictureFromLocation.selectImage(getApplicationContext(),data);
crop(Uri.parse("file://"+path));//
}
} else if (requestCode == PHOTO_REQUEST_GALLERY) {
if (data != null) {
//
String path = GetPictureFromLocation.getPath(getApplicationContext(), data.getData());
crop(Uri.parse("file://"+path));//
}
}else if (requestCode == PHOTO_REQUEST_CAMERA) {
crop(Uri.fromFile(tempFile)); //
}else if (requestCode == PHOTO_REQUEST_CUT) {
try {
Bitmap bitmap = BitmapFactory.decodeFile(tempFile.getPath());
Log.e("uri", Uri.fromFile(tempFile).toString());
head_image.setImageBitmap(bitmap);
} catch (Exception e) {
e.printStackTrace();
}
}
}
//クリップ画像ソース:
/**
*
*
* @function:
* @author:Jerry
* @date:2013-12-30
* @param uri
*/
private void crop(Uri uri) {
Log.e("URI", uri.getPath());
Log.e("URI", uri.toString());
//
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(uri, "image/*");
intent.putExtra("crop", "true");
// ,1:1
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
//
intent.putExtra("outputX", 150);
intent.putExtra("outputY", 150);
intent.putExtra("scale", true);//
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(tempFile));
//
intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
intent.putExtra("noFaceDetection", true);//
intent.putExtra("return-data", true);// true: uri,false: uri
startActivityForResult(intent, PHOTO_REQUEST_CUT);
}