我想做的事情似乎很简单,但经过几天的搜索,我无法弄明白。
我有一个应用程序,允许用户选择多个(最多5个)图像。我正在使用ImageView
。当用户点击ImageView
时,我想允许他们选择
我开始使用ACTION_GET_CONTENT
意图,这对于进入图库非常有用。所以我尝试使用ACTION_PICK_ACTIVITY
意图允许用户选择相机或图库:
Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
Intent gallIntent=new Intent(Intent.ACTION_GET_CONTENT);
gallIntent.setType("image/*");
Intent camIntent = new Intent("android.media.action.IMAGE_CAPTURE");
pickIntent.putExtra(Intent.EXTRA_INTENT, camIntent);
pickIntent.putExtra(Intent.EXTRA_INTENT, gallIntent)
pickIntent.putExtra(Intent.EXTRA_TITLE, "Select Source");
startActivityForResult(pickIntent, IMAGE_SELECTOR);
但似乎我只能添加一个EXTRA_INTENT
。菜单按预期显示,但唯一的选项是图库和文件....没有相机)。
有没有更好/更简单的方法来做到这一点,我错过了?谢谢你的帮助。
答案 0 :(得分:318)
如何启动单个Intent以从图库或相机中选择图像,或者注册以浏览文件系统的任何应用程序。
不是使用Intent选项列表创建一个Dialog,而是使用Intent.createChooser来访问各种“Camera”,“Gallery”甚至第三方的图形图标和短名称要好得多。文件系统浏览器应用程序,如“Astro”等。
这描述了如何使用标准选择器意图并添加其他意图。
private Uri outputFileUri;
private void openImageIntent() {
// Determine Uri of camera image to save.
final File root = new File(Environment.getExternalStorageDirectory() + File.separator + "MyDir" + File.separator);
root.mkdirs();
final String fname = Utils.getUniqueImageFilename();
final File sdImageMainDirectory = new File(root, fname);
outputFileUri = Uri.fromFile(sdImageMainDirectory);
// Camera.
final List<Intent> cameraIntents = new ArrayList<Intent>();
final Intent captureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
final PackageManager packageManager = getPackageManager();
final List<ResolveInfo> listCam = packageManager.queryIntentActivities(captureIntent, 0);
for(ResolveInfo res : listCam) {
final String packageName = res.activityInfo.packageName;
final Intent intent = new Intent(captureIntent);
intent.setComponent(new ComponentName(packageName, res.activityInfo.name));
intent.setPackage(packageName);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
cameraIntents.add(intent);
}
// Filesystem.
final Intent galleryIntent = new Intent();
galleryIntent.setType("image/*");
galleryIntent.setAction(Intent.ACTION_GET_CONTENT);
// Chooser of filesystem options.
final Intent chooserIntent = Intent.createChooser(galleryIntent, "Select Source");
// Add the camera options.
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, cameraIntents.toArray(new Parcelable[cameraIntents.size()]));
startActivityForResult(chooserIntent, YOUR_SELECT_PICTURE_REQUEST_CODE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == YOUR_SELECT_PICTURE_REQUEST_CODE) {
final boolean isCamera;
if (data == null) {
isCamera = true;
} else {
final String action = data.getAction();
if (action == null) {
isCamera = false;
} else {
isCamera = action.equals(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
}
}
Uri selectedImageUri;
if (isCamera) {
selectedImageUri = outputFileUri;
} else {
selectedImageUri = data == null ? null : data.getData();
}
}
}
}
答案 1 :(得分:31)
您必须创建自己的选择器对话框,合并两个意图解析结果。
要执行此操作,您需要使用PackageManager.queryIntentActivities()查询PackageManager以获取两个原始意图,并为每个检索到的活动创建一个可能的Intent最终列表,其中包含一个新的Intent,如下所示:
List<Intent> yourIntentsList = new ArrayList<Intent>();
List<ResolveInfo> listCam = packageManager.queryIntentActivities(camIntent, 0);
for (ResolveInfo res : listCam) {
final Intent finalIntent = new Intent(camIntent);
finalIntent.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
yourIntentsList.add(finalIntent);
}
List<ResolveInfo> listGall = packageManager.queryIntentActivities(gallIntent, 0);
for (ResolveInfo res : listGall) {
final Intent finalIntent = new Intent(gallIntent);
finalIntent.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
yourIntentsList.add(finalIntent);
}
(我在这里直接写了这个,所以这可能无法编译)
然后,有关从列表创建自定义对话框的详细信息,请参阅https://developer.android.com/guide/topics/ui/dialogs.html#AlertDialog
答案 2 :(得分:22)
我找到了this。使用:
galleryIntent.setType("image/*");
galleryIntent.setAction(Intent.ACTION_GET_CONTENT);
其中一个意图向用户显示选择&#39;文件&#39;在Android 4中,我发现非常令人困惑。使用它来代替显示“画廊”#39;选项:
Intent pickIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
答案 3 :(得分:12)
我也有这个问题,我做的是创建一个AlertDialog并使用setItems()方法和DialogInterface监听器:
AlertDialog.Builder getImageFrom = new AlertDialog.Builder(Fotos.this);
getImageFrom.setTitle("Select:");
final CharSequence[] opsChars = {getResources().getString(R.string.takepic), getResources().getString(R.string.opengallery)};
getImageFrom.setItems(opsChars, new android.content.DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
if(which == 0){
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
}else
if(which == 1){
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,
getResources().getString(R.string.pickgallery)), SELECT_PICTURE);
}
dialog.dismiss();
}
});
答案 4 :(得分:11)
我已经合并了一些解决方案来制作一个完整的工具,用于从图库或相机中拾取图像。这些是ImagePicker util(也在Github lib)中的功能:
<强>截图:强>
编辑:这是一个代码片段,用于将Gallery和Camera应用程序合并为Intent。 您可以在ImagePicker util(也在Github lib)中看到完整的代码:
public static Intent getPickImageIntent(Context context) {
Intent chooserIntent = null;
List<Intent> intentList = new ArrayList<>();
Intent pickIntent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
Intent takePhotoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
takePhotoIntent.putExtra("return-data", true);
takePhotoIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(getTempFile(context)));
intentList = addIntentsToList(context, intentList, pickIntent);
intentList = addIntentsToList(context, intentList, takePhotoIntent);
if (intentList.size() > 0) {
chooserIntent = Intent.createChooser(intentList.remove(intentList.size() - 1),
context.getString(R.string.pick_image_intent_text));
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentList.toArray(new Parcelable[]{}));
}
return chooserIntent;
}
private static List<Intent> addIntentsToList(Context context, List<Intent> list, Intent intent) {
List<ResolveInfo> resInfo = context.getPackageManager().queryIntentActivities(intent, 0);
for (ResolveInfo resolveInfo : resInfo) {
String packageName = resolveInfo.activityInfo.packageName;
Intent targetedIntent = new Intent(intent);
targetedIntent.setPackage(packageName);
list.add(targetedIntent);
}
return list;
}
答案 5 :(得分:8)
此代码可以帮助您,因为有两个按钮用于相机,另一个按钮用于图库,图像将显示在ImageView中
https://github.com/siddhpuraamitr/Choose-Image-From-Gallery-Or-Camera
答案 6 :(得分:6)
对于那些在尝试使用图像选择时向上获得4.4错误的人可以使用下面的代码。
不是使用Intent选项列表创建一个Dialog,而是使用Intent.createChooser来访问各种&#39; Camera&#39;,&#的图形图标和短名称要好得多。 39;画廊&#39;甚至第三方文件系统浏览器应用程序,如“Astro&#39;等”
这描述了如何使用标准选择器意图并添加其他意图。
private void openImageIntent(){
// Determine Uri of camera image to save.
final File root = new File(Environment.getExternalStorageDirectory() + File.separator + "amfb" + File.separator);
root.mkdir();
final String fname = "img_" + System.currentTimeMillis() + ".jpg";
final File sdImageMainDirectory = new File(root, fname);
outputFileUri = Uri.fromFile(sdImageMainDirectory);
// Camera.
final List<Intent> cameraIntents = new ArrayList<Intent>();
final Intent captureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
final PackageManager packageManager = getPackageManager();
final List<ResolveInfo> listCam = packageManager.queryIntentActivities(captureIntent, 0);
for (ResolveInfo res : listCam){
final String packageName = res.activityInfo.packageName;
final Intent intent = new Intent(captureIntent);
intent.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
intent.setPackage(packageName);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
cameraIntents.add(intent);
}
//FileSystem
final Intent galleryIntent = new Intent();
galleryIntent.setType("image/");
galleryIntent.setAction(Intent.ACTION_GET_CONTENT);
// Chooser of filesystem options.
final Intent chooserIntent = Intent.createChooser(galleryIntent, "Select Source");
// Add the camera options.
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, cameraIntents.toArray(new Parcelable[]{}));
startActivityForResult(chooserIntent, CAMERA_CAPTURE_IMAGE_REQUEST_CODE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
if (requestCode == CAMERA_CAPTURE_IMAGE_REQUEST_CODE) {
final boolean isCamera;
if (data == null) {
isCamera = true;
} else {
final String action = data.getAction();
if (action == null) {
isCamera = false;
} else {
isCamera = action.equals(MediaStore.ACTION_IMAGE_CAPTURE);
}
}
Uri selectedImageUri;
if (isCamera) {
selectedImageUri = outputFileUri;
//Bitmap factory
BitmapFactory.Options options = new BitmapFactory.Options();
// downsizing image as it throws OutOfMemory Exception for larger
// images
options.inSampleSize = 8;
final Bitmap bitmap = BitmapFactory.decodeFile(selectedImageUri.getPath(), options);
preview.setImageBitmap(bitmap);
} else {
selectedImageUri = data == null ? null : data.getData();
Log.d("ImageURI", selectedImageUri.getLastPathSegment());
// /Bitmap factory
BitmapFactory.Options options = new BitmapFactory.Options();
// downsizing image as it throws OutOfMemory Exception for larger
// images
options.inSampleSize = 8;
try {//Using Input Stream to get uri did the trick
InputStream input = getContentResolver().openInputStream(selectedImageUri);
final Bitmap bitmap = BitmapFactory.decodeStream(input);
preview.setImageBitmap(bitmap);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
} else if (resultCode == RESULT_CANCELED){
// user cancelled Image capture
Toast.makeText(getApplicationContext(),
"User cancelled image capture", Toast.LENGTH_SHORT)
.show();
} else {
// failed to capture image
Toast.makeText(getApplicationContext(),
"Sorry! Failed to capture image", Toast.LENGTH_SHORT)
.show();
}
}
答案 7 :(得分:6)
这应该照顾Tina的null outputFileUri问题:
private static final String STORED_INSTANCE_KEY_FILE_URI = "output_file_uri";
@Override
public void onSaveInstanceState( Bundle outState ) {
super.onSaveInstanceState( outState );
if ( outputFileUri != null ) {
outState.putString( STORED_INSTANCE_KEY_FILE_URI, outputFileUri.toString() );
}
}
@Override
public void onViewStateRestored( Bundle savedInstanceState ) {
super.onViewStateRestored( savedInstanceState );
if ( savedInstanceState != null ) {
final String outputFileUriStr = savedInstanceState.getString( STORED_INSTANCE_KEY_FILE_URI );
if ( outputFileUriStr != null && !outputFileUriStr.isEmpty() ) {
outputFileUri = Uri.parse( outputFileUriStr );
}
}
}
注意:我在android.support.v4.app.Fragment中使用此代码,您的重写方法可能会根据您正在使用的Fragment / Activity版本而改变。
答案 8 :(得分:5)
您可以创建选项对话框
打开相机:
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT,
MediaStore.Images.Media.EXTERNAL_CONTENT_URI.toString());
if (cameraIntent.resolveActivity(getActivity().getPackageManager()) != null) {
startActivityForResult(cameraIntent, CAMERA_IMAGE);
}
打开图库:
if (Build.VERSION.SDK_INT <= 19) {
Intent i = new Intent();
i.setType("image/*");
i.setAction(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
startActivityForResult(i, GALLARY_IMAGE);
} else if (Build.VERSION.SDK_INT > 19) {
Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, GALLARY_IMAGE);
}
获取选择结果
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
if (requestCode == GALLARY_IMAGE) {
Uri selectedImageUri = data.getData();
String selectedImagePath = getRealPathFromURI(selectedImageUri);
} else if (requestCode == CAMERA_IMAGE) {
Bundle extras = data.getExtras();
Bitmap bmp = (Bitmap) extras.get("data");
SaveImage(bmp);
}
}
}
public String getRealPathFromURI(Uri uri) {
if (uri == null) {
return null;
}
String[] projection = {MediaStore.Images.Media.DATA};
Cursor cursor = getActivity().getContentResolver().query(uri, projection, null, null, null);
if (cursor != null) {
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
return uri.getPath();
}
保存捕获图像的方法
private void SaveImage(final Bitmap finalBitmap) {
Thread t = new Thread(new Runnable() {
@Override
public void run() {
String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/Captured Images/");
if (!myDir.exists())
myDir.mkdirs();
String fname = "/image-" + System.currentTimeMillis() + ".jpg";
File file = new File(myDir, fname);
try {
FileOutputStream out = new FileOutputStream(file);
finalBitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
out.flush();
out.close();
localImagePath = myDir + fname;
} catch (Exception e) {
e.printStackTrace();
}
}
});
t.start();
}
答案 9 :(得分:2)
解决了太大的图像问题并避免内存不足。
private static final int SELECT_PICTURE = 0;
private static final int REQUEST_CAMERA = 1;
private ImageView mImageView;
private void selectImage() {
final CharSequence[] items = {"Take Photo", "Choose from Library",
"Cancel"};
AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
builder.setTitle("Add Photo!");
builder.setItems(items, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int item) {
if (items[item].equals("Take Photo")) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File f = new File(android.os.Environment
.getExternalStorageDirectory(), "temp.jpg");
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
startActivityForResult(intent, REQUEST_CAMERA);
} else if (items[item].equals("Choose from Library")) {
Intent intent = new Intent(
Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.setType("image/*");
startActivityForResult(
Intent.createChooser(intent, "Select File"),
SELECT_PICTURE);
} else if (items[item].equals("Cancel")) {
dialog.dismiss();
}
}
});
builder.show();
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
if (requestCode == REQUEST_CAMERA) {
File f = new File(Environment.getExternalStorageDirectory()
.toString());
for (File temp : f.listFiles()) {
if (temp.getName().equals("temp.jpg")) {
f = temp;
break;
}
}
try {
Bitmap bm;
BitmapFactory.Options btmapOptions = new BitmapFactory.Options();
btmapOptions.inSampleSize = 2;
bm = BitmapFactory.decodeFile(f.getAbsolutePath(),
btmapOptions);
// bm = Bitmap.createScaledBitmap(bm, 70, 70, true);
mImageView.setImageBitmap(bm);
String path = android.os.Environment
.getExternalStorageDirectory()
+ File.separator
+ "test";
f.delete();
OutputStream fOut = null;
File file = new File(path, String.valueOf(System
.currentTimeMillis()) + ".jpg");
fOut = new FileOutputStream(file);
bm.compress(Bitmap.CompressFormat.JPEG, 85, fOut);
fOut.flush();
fOut.close();
} catch (Exception e) {
e.printStackTrace();
}
} else if (requestCode == SELECT_PICTURE) {
Uri selectedImageUri = data.getData();
String tempPath = getPath(selectedImageUri, this.getActivity());
Bitmap bm;
btmapOptions.inSampleSize = 2;
BitmapFactory.Options btmapOptions = new BitmapFactory.Options();
bm = BitmapFactory.decodeFile(tempPath, btmapOptions);
mImageView.setImageBitmap(bm);
}
}
}
public String getPath(Uri uri, Activity activity) {
String[] projection = {MediaStore.MediaColumns.DATA};
Cursor cursor = activity
.managedQuery(uri, projection, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
答案 10 :(得分:1)
添加我的解决方案 - 它将返回一个回调,无论是来自相机还是来自厨房的意图:
public class ImagePickerManager extends BaseAdapter {
private List<ResolveInfo> mApplications;
private TreeSet<Integer> mImageCaptureIntents;
private TreeSet<Integer> mImagePickerIntents;
private Context mContext;
private final ImagePickerManagerListener listener;
private static enum intentType {
choosePhoto,
takePhoto,
unknown;
public int getIntValue() {
switch (this) {
case choosePhoto:
return 0;
case takePhoto:
return 1;
case unknown:
return 2;
}
return 0;
}
}
public interface ImagePickerManagerListener {
void onChooseImage(Intent intent);
void onCaptureImage(Intent intent);
}
public ImagePickerManager(Context context,ImagePickerManagerListener listenr) {
this.mContext = context;
this.listener = listenr;
mImageCaptureIntents = new TreeSet<>();
mImagePickerIntents = new TreeSet<>();
//Picking photo intent
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");
mApplications = mContext.getPackageManager().queryIntentActivities(intent, 0);
int index = 0;
for (int i = 0; i < mApplications.size(); i++) {
mImagePickerIntents.add(index);
index++;
}
//Capture photo intent
intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
List<ResolveInfo> resolveInfoList = mContext.getPackageManager().queryIntentActivities(intent, 0);
mApplications.addAll(resolveInfoList);
for (int i = 0; i < mApplications.size(); i++) {
mImageCaptureIntents.add(index);
index++;
}
}
public static void openChooseAndCaptureImageDialog(final Context context, final ImagePickerManagerListener listener) {
Log.d("openChooseAndCaptureImageDialog", "enter");
final AlertDialog.Builder builder = new AlertDialog.Builder(context);
final ImagePickerManager imagePickerManager = new ImagePickerManager(context,listener);
builder.setTitle(context.getString(R.string.image_picker_dialog_box_title));
builder.setAdapter(imagePickerManager, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialoginterface, int i) {
ResolveInfo resolveInfo = (ResolveInfo) imagePickerManager.getItem(i);
Intent pickerIntent = imagePickerManager.getIntentForPackage(context,resolveInfo,i);
switch (imagePickerManager.getIntentType(i)){
case choosePhoto:
listener.onChooseImage(pickerIntent);
break;
case takePhoto:
listener.onCaptureImage(pickerIntent);
break;
case unknown:
break;
}
}
});
builder.setCancelable(true);
builder.setInverseBackgroundForced(true);
AlertDialog dialog = builder.create();
dialog.show();
}
private intentType getIntentType(int index) {
if (mImageCaptureIntents.contains(index)) {
return intentType.takePhoto;
} else if(mImagePickerIntents.contains(index)) {
return intentType.choosePhoto;
}
return intentType.unknown;
}
private Intent getIntentForPackage(Context context, ResolveInfo info,int index) {
Intent intent = context.getPackageManager().getLaunchIntentForPackage(info.activityInfo.packageName);
ComponentName chosenName = new ComponentName(
info.activityInfo.packageName,
info.activityInfo.name);
intent.setComponent(chosenName);
intent.setFlags(Intent.FLAG_ACTIVITY_TASK_ON_HOME);
if (mImageCaptureIntents.contains(index)) {
intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
} else if(mImagePickerIntents.contains(index)) {
intent.setType("image/*");
intent.setAction(Intent.ACTION_PICK);
}
return intent;
}
@Override
public int getCount() {
return mApplications.size();
}
@Override
public Object getItem(int position) {
return mApplications.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ResolveInfo item = mApplications.get(position);
if (convertView == null) {
TextView applicationTextView = new TextView(mContext);
LayoutParams param = new LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT);
applicationTextView.setLayoutParams(param);
final int horizontalPadding = (int) FVRGeneralUtils.convertDpToPx(mContext, 15);
final int verticalPadding = (int) FVRGeneralUtils.convertDpToPx(mContext, 5);
applicationTextView.setPadding(horizontalPadding,verticalPadding, horizontalPadding, verticalPadding);
applicationTextView.setGravity(android.view.Gravity.CENTER_VERTICAL);
Resources.Theme th = mContext.getTheme();
TypedValue tv = new TypedValue();
if (th.resolveAttribute(android.R.attr.textAppearanceMedium, tv, true)) {
applicationTextView.setTextAppearance(mContext, tv.resourceId);
}
applicationTextView.setMinHeight((int) FVRGeneralUtils.convertDpToPx(mContext, 25));
applicationTextView.setCompoundDrawablePadding((int) FVRGeneralUtils.convertDpToPx(mContext, 7));
convertView = applicationTextView;
}
TextView textView = (TextView) convertView;
textView.setText(item.loadLabel(mContext.getPackageManager()));
textView.setCompoundDrawablesWithIntrinsicBounds(item.loadIcon(mContext.getPackageManager()), null, null, null);
return textView;
} }
答案 11 :(得分:1)
你可以试试这个:
要打开图库:
private void browseImage() {
try {
Intent galleryIntent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(galleryIntent, GALLERY_IMAGE_PICK); //GALLERY_IMAGE_PICK it is a string
} catch (Exception e) {}
}
打开相机:
private void captureImage() {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
// start the image capture Intent
startActivityForResult(intent, CAMERA_CAPTURE_IMAGE_REQUEST_CODE);
}
答案 12 :(得分:0)
以大卫的回答为基础,我在private enum Outcome {
camera, singleLibrary, multipleLibrary, unknown
}
/**
* Returns a List<Uri> containing the image uri(s) chosen by the user
*
* @param data The data intent coming from the onActivityResult()
* @param cameraUri The uri that had been passed to the intent when the chooser was invoked.
* @return A List<Uri>, never null.
*/
public List<Uri> getPicturesUriFromIntent(Intent data, Uri cameraUri) {
Outcome outcome = Outcome.unknown;
if (data == null || (data.getData() == null && data.getClipData() == null)) {
outcome = Outcome.camera;
} else if (data.getData() != null && data.getClipData() == null) {
outcome = Outcome.singleLibrary;
} else if (data.getData() == null) {
outcome = Outcome.multipleLibrary;
} else {
final String action = data.getAction();
if (action != null && action.equals(MediaStore.ACTION_IMAGE_CAPTURE)) {
outcome = Outcome.camera;
}
}
// list the uri(s) we got back
List<Uri> uris = new ArrayList<>();
switch (outcome) {
case camera:
uris.add(cameraUri);
break;
case singleLibrary:
uris.add(data.getData());
break;
case multipleLibrary:
final ClipData clipData = data.getClipData();
for (int i = 0; i < clipData.getItemCount(); i++) {
ClipData.Item item = clipData.getItemAt(i);
uris.add(item.getUri());
}
break;
}
return uris;
}
部分的两便士。它负责5.1.1中引入的更改,并检测用户是否从库中选择了单个或多个图像。
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim FileName As String = "C:\Users\Mike\Documents\SomeFile.txt"
Dim lines As New List(Of String)(File.ReadAllLines(FileName))
AddParameterToSection(lines, "551", "-apple")
AddParameterToSection(lines, "552", "-orange")
File.WriteAllLines(FileName, lines)
End Sub
Private Sub AddParameterToSection(ByVal lines As List(Of String), ByVal sectionID As String, ByVal parameter As String)
Static DoubleQuote As String = Chr(34)
Static Parameters As String = DoubleQuote & "parameters" & DoubleQuote
Static PairOfDoubleQoutes As String = DoubleQuote & DoubleQuote
Dim header As String = DoubleQuote & sectionID & DoubleQuote
Dim index As Integer = lines.FindIndex(Function(x) x.Trim() = header)
If index <> -1 Then
Dim endBracket As Integer = lines.FindIndex(index, Function(x) x.Trim() = "}")
Dim paramIndex As Integer = lines.FindIndex(index, Function(x) x.Trim().StartsWith(Parameters))
If paramIndex <> -1 AndAlso paramIndex < endBracket Then
If lines(paramIndex).EndsWith(PairOfDoubleQoutes) Then
lines(paramIndex) = lines(paramIndex).Replace(PairOfDoubleQoutes, DoubleQuote & parameter & DoubleQuote)
Else
lines(paramIndex) = lines(paramIndex).TrimEnd(DoubleQuote) & " " & parameter & DoubleQuote
End If
End If
End If
End Sub
答案 13 :(得分:0)
使用AlertDialog和Intent.ACTION_PICK
这很简单 //camOption is a string array contains two items (Camera, Gallery)
AlertDialog.Builder builder = new AlertDialog.Builder(CarPhotos.this);
builder.setTitle(R.string.selectSource)
.setItems(R.array.imgOption, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which)
{
if (which==0) {
Intent intent = new Intent(this, CameraActivity.class);
startActivityForResult(intent, REQ_CAMERA_IMAGE); }
if (which==1) {
Intent i = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);
}
}
});
builder.create();
builder.show();
答案 14 :(得分:0)
试试这种方式
final CharSequence[] items = { "Take Photo", "Choose from Library",
"Cancel" };
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Add Photo!");
builder.setItems(items, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int item) {
if (items[item].equals("Take Photo")) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File f = new File(android.os.Environment
.getExternalStorageDirectory(), "temp.jpg");
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
startActivityForResult(intent, REQUEST_CAMERA);
} else if (items[item].equals("Choose from Library")) {
Intent intent = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.setType("image/*");
startActivityForResult(
Intent.createChooser(intent, "Select File"),
SELECT_FILE);
} else if (items[item].equals("Cancel")) {
dialog.dismiss();
}
}
});
builder.show();
然后创建onactivityresult方法并执行类似的操作
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
if (requestCode == REQUEST_CAMERA) {
File f = new File(Environment.getExternalStorageDirectory()
.toString());
for (File temp : f.listFiles()) {
if (temp.getName().equals("temp.jpg")) {
f = temp;
break;
}
}
try {
Bitmap bm;
BitmapFactory.Options btmapOptions = new BitmapFactory.Options();
bm = BitmapFactory.decodeFile(f.getAbsolutePath(),
btmapOptions);
// bm = Bitmap.createScaledBitmap(bm, 70, 70, true);
ivImage.setImageBitmap(bm);
String path = android.os.Environment
.getExternalStorageDirectory()
+ File.separator
+ "Phoenix" + File.separator + "default";
f.delete();
OutputStream fOut = null;
File file = new File(path, String.valueOf(System
.currentTimeMillis()) + ".jpg");
try {
fOut = new FileOutputStream(file);
bm.compress(Bitmap.CompressFormat.JPEG, 85, fOut);
fOut.flush();
fOut.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
} else if (requestCode == SELECT_FILE) {
Uri selectedImageUri = data.getData();
String tempPath = getPath(selectedImageUri, MainActivity.this);
Bitmap bm;
BitmapFactory.Options btmapOptions = new BitmapFactory.Options();
bm = BitmapFactory.decodeFile(tempPath, btmapOptions);
ivImage.setImageBitmap(bm);
}
}
}
请参阅此http://www.theappguruz.com/blog/android-take-photo-camera-gallery-code-sample
答案 15 :(得分:0)
在Android中为图片选择相机或图库
我曾在相机或图库选择上努力工作,并为此工作创建了一些实用工具类。 使用这些类 &#39;选择图像相机或图库太容易了 你的开发只花了5-10分钟。
步骤1:在代码中添加这些类。
ImagePickerUtils: - http://www.codesend.com/view/f8f7c637716bf1c693d1490635ed49b3/
BitmapUtils: - http://www.codesend.com/view/81c1c2a3f39f1f7e627f01f67be282cf/
ConvertUriToFilePath: - http://www.codesend.com/view/f4668a29860235dd1b66eb419c5a58b5/
MediaUtils: - https://codeshare.io/5vKEMl
我们需要在menifest中添加这些权限:
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
这个类函数(checkAndRequestPermissions)会自动检查Android-Marshmallow和Android中的权限 - Nougat。
<强>步骤2。调用相机类以启动相机意图:
//Create a global veriable .
private Uri mCameraUri;
private static final int CAMERA_REQUEST_CODE = 100;
// Call this function when you wants to select or capture an Image.
mCameraUri = ImagePickerUtils.createTakePictureIntent(this, CAMERA_REQUEST_CODE);
步骤3:在您的活动中添加onActivityResult以从Intent接收数据
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
Uri fileUri = ImagePickerUtils.getFileUriOfImage(this, data, mCameraUri);
try {
Bitmap bitmap = null;
if (CAMERA_REQUEST_CODE == requestCode) {
bitmap = new BitmapUtils().getDownsampledBitmap(this, fileUri, imageView.getWidth(), imageView.getHeight());
}
if (bitmap != null)
imageView.setImageBitmap(bitmap);
} catch (Exception e) {
e.printStackTrace();
}
}
}
我希望对您有所帮助,如果有任何人提出改进这些课程的建议,请在评论中添加您的评论。
答案 16 :(得分:0)
根据David Manpearl回答
https://stackoverflow.com/a/12347567/7226732
我们只需修改 onActivityResult(),如
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == YOUR_SELECT_PICTURE_REQUEST_CODE) {
final boolean isCamera;
if (data == null) {
isCamera = true;
} else {
final String action = data.getAction();
if (action == null) {
isCamera = false;
} else {
isCamera = action.equals(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
}
}
Uri selectedImageUri;
if (isCamera) {
selectedImageUri = fileUri;
try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), selectedImageUri);
Toast.makeText(CreateWaterType.this, "Image Saved!", Toast.LENGTH_SHORT).show();
image_view.setImageBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(CreateWaterType.this, "Failed!", Toast.LENGTH_SHORT).show();
}
} else {
selectedImageUri = data == null ? null : data.getData();
try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), selectedImageUri);
Toast.makeText(CreateWaterType.this, "Image Saved!", Toast.LENGTH_SHORT).show();
image_view.setImageBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(CreateWaterType.this, "Failed!", Toast.LENGTH_SHORT).show();
}
}
}
}
}
并在图像视图中设置捕获或拾取图像。