我正试图通过意图启动Android camaera,拍照并返回URI。应该非常简单,毕竟有关于如何在SO上执行此操作的负载和负载。
然而,我正在使用带有ICS的星系S3,并发现当我调用相机意图时它会把我带到画廊而不是相机。
这是我尝试过的代码。
首先是一个简单的:
public static final int REQUEST_CODE_CAMERA = 1337;
.
.
.
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, REQUEST_CODE_CAMERA);
这不起作用。我试过Vogella.com
的例子private static final int REQUEST_CODE = 1;
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
startActivityForResult(intent, REQUEST_CODE);
我还读过人们建议不同的请求代码,例如1888,1337和2500.我可以使用基于通用请求代码或框架的静态int吗?或者这些代码是众多SO red herrings之一,因此可以使用任何代码吗?
BTW我知道S3不能与MediaStore.EXTRA_OUTPUT一起使用的错误。我需要的不是这个。我已经知道如何克服这个障碍了。
[编辑]
* *请注意
对于那些阅读此方向的人来说,这可能是一个严重的问题,需要在使用相机意图时注意。虽然启动相机的活动将其方向锁定在清单中。当设备处于纵向模式时,它会在 OnActivityResult 之前和之后调用 OnCreate ;因此擦除了类全局变量。解决方案是通过* onSaveInstanceStat * e方法保存所有类全局变量,并在 OnCreate 中使用这些来重新加载并在视图中显示图像。根据从SO收集到的信息,并非所有设备都会执行此操作,但每台设备的设置一致。我的猜测是活动的内存管理依赖于可用的硬件和Android平台。我真的希望情况并非如此,但Android是Android。
[/编辑]
答案 0 :(得分:1)
使用相机我使用以下意图:
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
答案 1 :(得分:0)
MainActivity.java
public class MainActivity extends Activity {
private static final int CAMERA_REQUEST = 1888;
private ImageView imageView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.imageView = (ImageView)this.findViewById(R.id.imageView1);
Button photoButton = (Button) this.findViewById(R.id.button1);
photoButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
File imagesFolder = new File(Environment.getExternalStorageDirectory(), "MyCameraImages");
imagesFolder.mkdirs();
File image = new File(imagesFolder, "image.jpg");
Uri uriSavedImage = Uri.fromFile(image);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(photo);
}
}
}
activity_main.xml中
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_marginBottom="79dp" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="26dp"
android:text="StartCamera" />
</RelativeLayout>
快照保存在图库中的图像
答案 2 :(得分:0)
android.media.action.IMAGE_CAPTURE
和
android.provider.MediaStore.ACTION_IMAGE_CAPTURE
是同一件事。变量android.provider.MediaStore.ACTION_IMAGE_CAPTURE 指的是上面的字符串。引用变量而不是字符串的原因是为了确保字符串没有变化。如果字符串改变,android人员也应该改变常量,你应该能够知道你指的是错误的常量,但对于字符串,编译时不会出现错误。 我希望它能够清除。
答案 3 :(得分:0)
从图库中选择照片并拍摄新照片的代码也受SDK 24或更高版本的支持
活动
private File filePathImageCamera;
private Uri imagePath;
private static final int IMAGE_GALLERY_REQUEST = 2;
private static final int IMAGE_CAMERA_REQUEST = 3;
private String imageFilePath;
private void selectImage() {
final CharSequence[] options = {"Take Photo", "Choose from Gallery", "Cancel"};
AlertDialog.Builder builder = new AlertDialog.Builder(DocumentActivity.this);
builder.setTitle("Upload Photo!");
builder.setItems(options, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int item) {
if (options[item].equals("Take Photo")) {
photoCameraIntent();
} else if (options[item].equals("Choose from Gallery")) {
photoGalleryIntent();
} else if (options[item].equals("Cancel")) {
dialog.dismiss();
}
}
});
builder.show();
}
private void photoCameraIntent() {
Intent pictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
Uri uri = null;
try {
filePathImageCamera = createImageFile();
} catch (IOException ex) {
// Error occurred while creating the File
}
if (pictureIntent.resolveActivity(getPackageManager()) != null) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
//Create a file to store the image
if (filePathImageCamera != null) {
Uri photoURI = FileProvider.getUriForFile(this, BuildConfig.APPLICATION_ID + ".provider", filePathImageCamera);
pictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
photoURI);
startActivityForResult(pictureIntent,
IMAGE_CAMERA_REQUEST);
}
} else {
if (filePathImageCamera != null) {
uri = Uri.fromFile(filePathImageCamera);
pictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
startActivityForResult(pictureIntent, IMAGE_CAMERA_REQUEST);
}
}
}
}
private File createImageFile() throws IOException {
String timeStamp =
new SimpleDateFormat("yyyyMMdd_HHmmss",
Locale.getDefault()).format(new Date());
String imageFileName = "IMG_" + timeStamp + "_";
File storageDir =
getExternalFilesDir(Environment.DIRECTORY_PICTURES);
// Create the storage directory if it does not exist
if (!storageDir.exists() && !storageDir.mkdirs()){
Log.d("error", "failed to create directory");
}
File image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
imageFilePath = image.getAbsolutePath();
return image;
}
private void photoGalleryIntent() {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Get Image From"), IMAGE_GALLERY_REQUEST);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == IMAGE_GALLERY_REQUEST) {
if (resultCode == RESULT_OK) {
imagePath = data.getData();
Glide.with(MainActivity.this).load(imagePath).into(imageview);
}
} else if (requestCode == IMAGE_CAMERA_REQUEST) {
if (resultCode == RESULT_OK) {
if (filePathImageCamera != null && filePathImageCamera.exists()) {
imagePath = Uri.fromFile(filePathImageCamera);
Glide.with(MainActivity.this).load(imagePath).into(imageview);
}
}
}
}
清单
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<application>
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths" />
</provider>
</application>
在res / xml目录中创建新文件provider_paths.xml
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="my_images"
path="Android/data/your_package_name/files/Pictures" />
</paths>
注意-需要申请许可