当我点击打开相机的弹出窗口时。如果我拍摄的照片保存得很好。
但是,如果我只是在相机&退出而不添加照片,而不是图片保存,如图片所示。
这是拍照
builder.setItems(options, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int item) {
if (options[item].equals(getString(R.string.label_popup_takephoto))) //take photo
{
Intent takePhotoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
Uri mPhotoUri;
if(bal.hasStorage(true))
mPhotoUri = getActivity().getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, new ContentValues());
else
mPhotoUri = getActivity().getContentResolver().insert(MediaStore.Images.Media.INTERNAL_CONTENT_URI, new ContentValues());
takePhotoIntent.putExtra(MediaStore.EXTRA_OUTPUT, mPhotoUri);
startActivityForResult(takePhotoIntent, 1);
}
这适用于存储的图像
if (resultCode == -1) {// Camera
Bitmap addphoto = BitmapFactory.decodeResource(getActivity().getResources(), R.drawable.user);
if (requestCode == 1) {
Cursor cursor;
String imagePath = null;
cursor = getActivity().getContentResolver().query(Media.EXTERNAL_CONTENT_URI,
new String[] { Media.DATA, Media.DATE_ADDED,MediaStore.Images.ImageColumns.ORIENTATION },Media.DATE_ADDED, null, "date_added ASC");
if (cursor != null && cursor.moveToFirst()){
do
{
imagePath = cursor.getString(cursor.getColumnIndex(Media.DATA));
}while(cursor.moveToNext());
}
cursor.close();
Bitmap yourSelectedImage=processimage(imagePath);
//Bitmap yourSelectedImage = getScaledBitmap(imagePath, 500, 500);
if (yourSelectedImage != null) {
if (imgprofile != null) {
imgprofile.setImageBitmap(yourSelectedImage);
picimagepath=imagePath;
bmp=yourSelectedImage;
}
else{
imgprofile.setImageBitmap(addphoto);
}
}
Hase Storage
public boolean hasStorage(boolean requireWriteAccess)
{
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
return true;
} else if (!requireWriteAccess
&& Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
return true;
}
return false;
}
我的错误在哪里?
答案 0 :(得分:0)
只需检查结果代码是否取消,onActivityResult中的请求代码为1,然后从给定的uri位置删除文件:
if(resultCode ==RESULT_CANCELED){
if(requestCode== 1) {
File file = new File(getAbsolutePath(mPhotoUri));
if (file.exists()) {
file.delete();
}
}
}
public String getAbsolutePath(Uri uri) {
if(Build.VERSION.SDK_INT >= 19){
String id = uri.getLastPathSegment().split(":")[1];
final String[] imageColumns = {MediaStore.Images.Media.DATA };
final String imageOrderBy = null;
Cursor imageCursor = getContentResolver().query(uri, imageColumns,MediaStore.Images.Media._ID + "="+id, null, imageOrderBy);
if (imageCursor.moveToFirst()) {
return imageCursor.getString(imageCursor.getColumnIndex(MediaStore.Images.Media.DATA));
}else{
return null;
}
}else{
String[] projection = { MediaColumns.DATA };
Cursor cursor = getContentResolver().query(uri, projection, null, null, null);
if (cursor != null) {
int column_index = cursor.getColumnIndexOrThrow(MediaColumns.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
} else
return null;
}
}
示例:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == -1) {// Camera
Bitmap addphoto = BitmapFactory.decodeResource(getActivity().getResources(), R.drawable.user);
if (requestCode == 1) {
Cursor cursor;
String imagePath = null;
cursor = getActivity().getContentResolver().query(Media.EXTERNAL_CONTENT_URI,new String[]{Media.DATA, Media.DATE_ADDED, MediaStore.Images.ImageColumns.ORIENTATION}, Media.DATE_ADDED, null, "date_added ASC");
if (cursor != null && cursor.moveToFirst()) {
do {
imagePath = cursor.getString(cursor.getColumnIndex(Media.DATA));
} while (cursor.moveToNext());
}
cursor.close();
Bitmap yourSelectedImage = processimage(imagePath);
//Bitmap yourSelectedImage = getScaledBitmap(imagePath, 500, 500);
if (yourSelectedImage != null) {
if (imgprofile != null) {
imgprofile.setImageBitmap(yourSelectedImage);
picimagepath = imagePath;
bmp = yourSelectedImage;
} else {
imgprofile.setImageBitmap(addphoto);
}
}
}
}else if(resultCode == 0){
if(requestCode == 1){
File file = new File(getAbsolutePath(mPhotoUri));
if (file.exists()) {
file.delete();
}
}
}else{
super.onActivityResult(requestCode, resultCode, data);
}
}