活动结果可能有问题吗?
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException e) {
e.printStackTrace();
}
String authorities = getApplicationContext().getPackageName() + ".fileprovider";
Uri imageUri = FileProvider.getUriForFile(camtest.this, authorities, photoFile);
intent.putExtra(MediaStore.EXTRA_OUTPUT,imageUri);
if (photoFile != null) {
imageUri = FileProvider.getUriForFile(camtest.this,
BuildConfig.APPLICATION_ID + ".fileprovider",
photoFile);
Log.i("Uri", imageUri.toString());
// takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
}
else{
Log.i("Uri", "none");
}
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(photoFile)));
startActivityForResult(intent, 7);
}
});
文件创建方法:
File createImageFile() throws IOException {
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "LetsTalk_img" + timeStamp;
File storageDirectory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(imageFileName,".jpg", storageDirectory);
mImageFileLocation = image.getAbsolutePath();
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(image)));
return image;
}
和onactivityresult: 这是我的错误吗?
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 7 && resultCode == RESULT_OK) {
Bitmap bitmap = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(bitmap);
}
}
提前致谢
答案 0 :(得分:0)
您只需将cameraIntent()
方法放入按钮点击功能。
private void cameraIntent() {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, 0);
}
然后贴上这个。
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
if (requestCode == 0){
onCaptureImageResult(data);
}
}
}
用于设置缩略图的传递ImageView变量中的数据,如下所示:
private void onCaptureImageResult(Intent data) {
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
thumbnail.compress(Bitmap.CompressFormat.JPEG, 90, bytes);
byteArray = bytes.toByteArray();
encodedImage = Base64.encodeToString(byteArray, Base64.DEFAULT);
File destination = new File(Environment.getExternalStorageDirectory(),
System.currentTimeMillis() + ".jpg");
FileOutputStream fo;
try {
destination.createNewFile();
fo = new FileOutputStream(destination);
fo.write(bytes.toByteArray());
fo.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
imagebox.setImageBitmap(thumbnail);
}