我正在尝试拍照并将其保存到自定义位置 -
public void SavePhoto(View view){
Intent imageIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
File imagesFolder = new File(Environment.getExternalStorageDirectory(), "WorkingWithPhotosApp");
imagesFolder.mkdirs();
File image = new File(imagesFolder, "QR_" + timeStamp + ".png");
Uri uriSavedImage = Uri.fromFile(image);
imageIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);
startActivityForResult(imageIntent, REQUEST_IMAGE_CAPTURE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(data==null){
Toast.makeText(MainActivity.this, "Data is null", Toast.LENGTH_SHORT).show();
}
else{
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
Bundle extras = data.getExtras();
Bitmap imageBitmap = (Bitmap) extras.get("data");
ImageView mImageView=(ImageView)findViewById(R.id.imageView);
mImageView.setImageBitmap(imageBitmap);
}
}
}
onActivityResult()中的数据为空。我错过了什么?
答案 0 :(得分:2)
你在这里预先插入了一个uri:
imageIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);
因此,当您获得Activity.RESULT_OK
时,只需按已知网址加载拍摄的照片即可。然后,您可以设置路径onActivityResult
,如下所示,但您需要转换为位图。
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
// Convert here your uri to bitmap then set it.//
mImageView.setImageBitmap(YOUR_BITMAP);
}
答案 1 :(得分:1)
文件imagesFolder =新文件(Environment.getExternalStorageDirectory(),“WorkingWithPhotosApp”);
我认为您需要首先检查您的目录是否存在。这就是我执行此任务的方式。在我的情况下,我在默认的DCIM
目录中创建一个文件夹。
private void dispatchTakePictureIntent(int actionCode) {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File f = null;
try {
f = setUpPhotoFile();
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
} catch (IOException e) {
e.printStackTrace();
f = null;
}
startActivityForResult(takePictureIntent, actionCode);
}
private File setUpPhotoFile() throws IOException {
File f = createImageFile();
return f;
}
private File createImageFile() throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "IMG_" + timeStamp + "_";
File albumF = getAlbumDir();
File imageF = File.createTempFile(imageFileName, ".jpg", albumF);
return imageF;
}
private File getAlbumDir() {
File storageDir = null;
if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
storageDir = getAlbumStorageDir(PHOTO_ALBUM_NAME);
if (storageDir != null) {
if (! storageDir.mkdirs()) {
if (! storageDir.exists()){
Log.d("Camera", "failed to create directory");
return null;
}
}
}
} else {
Log.v(getString(R.string.app_name), "External storage is not mounted READ/WRITE.");
}
return storageDir;
}
public File getAlbumStorageDir(String albumName) {
return new File (
Environment.getExternalStorageDirectory()
+ "/dcim/"
+ albumName);
}
希望这有帮助!
答案 2 :(得分:0)
试试这样:
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
Strng imagePath = uriSavedImage.getPath(); // uriSavedImage - make it global
}
答案 3 :(得分:0)
我遇到了和你一样的问题,所以我决定实施给定here
的解决方案然后,在我自己的项目中,我做了那个
首先:在清单中创建提供程序,与指南所说的相同
第二次:创建一个类名LegacyCompatCursorWrapper(ps。参见指南或与我联系,我可以发送代码),你需要这个类,在下面的步骤中。
第三:创建此课程LegacyCompatFileProvider
第四:在此之前
startActivityForResult(takePictureIntent, actionCode);
实施此
Intent i=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
Uri outputUri=FileProvider.getUriForFile(this, AUTHORITY, output);
i.putExtra(MediaStore.EXTRA_OUTPUT, outputUri);
if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.LOLLIPOP) {
i.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
}
else if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.JELLY_BEAN) {
ClipData clip=
ClipData.newUri(getContentResolver(), "A photo", outputUri);
i.setClipData(clip);
i.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
}
else {
List<ResolveInfo> resInfoList=
getPackageManager()
.queryIntentActivities(i, PackageManager.MATCH_DEFAULT_ONLY);
for (ResolveInfo resolveInfo : resInfoList) {
String packageName = resolveInfo.activityInfo.packageName;
grantUriPermission(packageName, outputUri,
Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
}
}
try {
startActivityForResult(i, CONTENT_REQUEST);
}
catch (ActivityNotFoundException e) {
Toast.makeText(this, R.string.msg_no_camera, Toast.LENGTH_LONG).show();
finish();
}
所以在onActivityResult()
if (requestCode == CONTENT_REQUEST) {
// if (resultCode == RESULT_OK) {
Intent i=new Intent(Intent.ACTION_VIEW);
Uri outputUri=FileProvider.getUriForFile(this, AUTHORITY, output);
i.setDataAndType(outputUri, "image/jpeg");
i.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
Bitmap thumbnail = null;
Bundle extras = i.getExtras();
try {
thumbnail = MediaStore.Images.Media.getBitmap(this.getContentResolver(),outputUri );
} catch (IOException e) {
e.printStackTrace();
}
mImageProfile.setImageBitmap(thumbnail);
saveImage(thumbnail);
}
mImageProfile
- &gt;是视图上的ImageView。
请记住,这是根据指南。