android 怎么调用手机相册
答案:1 悬赏:70 手机版
解决时间 2021-04-05 07:18
- 提问者网友:呐年旧曙光
- 2021-04-04 23:17
android 怎么调用手机相册
最佳答案
- 五星知识达人网友:酒者煙囻
- 2021-04-04 23:34
[java] view plain copy
private void doPickPhotoAction() {
Context context = EditContact.this;
// Wrap our context to inflate list items using correct theme
final Context dialogContext = new ContextThemeWrapper(context,
android.R.style.Theme_Light);
String cancel="返回";
String[] choices;
choices = new String[2];
choices[0] = getString(R.string.take_photo); //拍照
choices[1] = getString(R.string.pick_photo); //从相册中选择
final ListAdapter adapter = new ArrayAdapter(dialogContext,
android.R.layout.simple_list_item_1, choices);
final AlertDialog.Builder builder = new AlertDialog.Builder(
dialogContext);
builder.setTitle(R.string.attachToContact);
builder.setSingleChoiceItems(adapter, -1,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
switch (which) {
case 0:{
String status=Environment.getExternalStorageState();
if(status.equals(Environment.MEDIA_MOUNTED)){//判断是否有SD卡
doTakePhoto();// 用户点击了从照相机获取
}
else{
showToast("没有SD卡");
}
break;
}
case 1:
doPickPhotoFromGallery();// 从相册中去获取
break;
}
}
});
builder.setNegativeButton(cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
builder.create().show();
}
}
protected void doTakePhoto() {
try {
// Launch camera to take photo for selected contact
PHOTO_DIR.mkdirs();// 创建照片的存储目录
mCurrentPhotoFile = new File(PHOTO_DIR, getPhotoFileName());// 给新照的照片文件命名
final Intent intent = getTakePickIntent(mCurrentPhotoFile);
startActivityForResult(intent, CAMERA_WITH_DATA);
} catch (ActivityNotFoundException e) {
Toast.makeText(this, R.string.photoPickerNotFoundText,
Toast.LENGTH_LONG).show();
}
}
public static Intent getTakePickIntent(File f) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE, null);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
return intent;
}
private String getPhotoFileName() {
Date date = new Date(System.currentTimeMillis());
SimpleDateFormat dateFormat = new SimpleDateFormat(
"'IMG'_yyyy-MM-dd HH:mm:ss");
return dateFormat.format(date) + ".jpg";
}
// 请求Gallery程序
protected void doPickPhotoFromGallery() {
try {
// Launch picker to choose photo for selected contact
final Intent intent = getPhotoPickIntent();
startActivityForResult(intent, PHOTO_PICKED_WITH_DATA);
} catch (ActivityNotFoundException e) {
Toast.makeText(this, R.string.photoPickerNotFoundText1,
Toast.LENGTH_LONG).show();
}
}
private void doPickPhotoAction() {
Context context = EditContact.this;
// Wrap our context to inflate list items using correct theme
final Context dialogContext = new ContextThemeWrapper(context,
android.R.style.Theme_Light);
String cancel="返回";
String[] choices;
choices = new String[2];
choices[0] = getString(R.string.take_photo); //拍照
choices[1] = getString(R.string.pick_photo); //从相册中选择
final ListAdapter adapter = new ArrayAdapter
android.R.layout.simple_list_item_1, choices);
final AlertDialog.Builder builder = new AlertDialog.Builder(
dialogContext);
builder.setTitle(R.string.attachToContact);
builder.setSingleChoiceItems(adapter, -1,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
switch (which) {
case 0:{
String status=Environment.getExternalStorageState();
if(status.equals(Environment.MEDIA_MOUNTED)){//判断是否有SD卡
doTakePhoto();// 用户点击了从照相机获取
}
else{
showToast("没有SD卡");
}
break;
}
case 1:
doPickPhotoFromGallery();// 从相册中去获取
break;
}
}
});
builder.setNegativeButton(cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
builder.create().show();
}
}
protected void doTakePhoto() {
try {
// Launch camera to take photo for selected contact
PHOTO_DIR.mkdirs();// 创建照片的存储目录
mCurrentPhotoFile = new File(PHOTO_DIR, getPhotoFileName());// 给新照的照片文件命名
final Intent intent = getTakePickIntent(mCurrentPhotoFile);
startActivityForResult(intent, CAMERA_WITH_DATA);
} catch (ActivityNotFoundException e) {
Toast.makeText(this, R.string.photoPickerNotFoundText,
Toast.LENGTH_LONG).show();
}
}
public static Intent getTakePickIntent(File f) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE, null);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
return intent;
}
private String getPhotoFileName() {
Date date = new Date(System.currentTimeMillis());
SimpleDateFormat dateFormat = new SimpleDateFormat(
"'IMG'_yyyy-MM-dd HH:mm:ss");
return dateFormat.format(date) + ".jpg";
}
// 请求Gallery程序
protected void doPickPhotoFromGallery() {
try {
// Launch picker to choose photo for selected contact
final Intent intent = getPhotoPickIntent();
startActivityForResult(intent, PHOTO_PICKED_WITH_DATA);
} catch (ActivityNotFoundException e) {
Toast.makeText(this, R.string.photoPickerNotFoundText1,
Toast.LENGTH_LONG).show();
}
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯