【问题标题】:Currupted image saving android损坏的图像保存android
【发布时间】:2023-03-19 22:55:01
【问题描述】:
  1. 当我点击打开相机的弹出窗口时。如果我拍摄保存良好的照片。

  2. 但如果我只进入相机并退出而不添加照片,则图像会像给定的图片一样保存。

.

  1. 如果添加任何新图像,则所有损坏的图像都将替换为新图像,例如第二张图像

.

这是用来拍照的

   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 存储

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;  
} 

我的错误在哪里?

【问题讨论】:

标签: android


【解决方案1】:

简单地检查结果代码是取消,请求代码在 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);
     }
 }

【讨论】:

  • 这意味着我只是把这个文件 file = new File(mPhotoUri); if (file.exists()) { file.delete(); } 在我的代码中.. 我已经声明 (if(resultCode == -1) {// Camera) 取消结果
  • RESULT_OK 常量值为 -1,RESULT_CANCELED 常量值为 0。
  • 对不起兄弟..但我在android中很新鲜......根据你的代码,我在结果代码和文件内容代码中将-1更改为0,文件内容代码将把我的代码放在光标下方关闭?请给我一个想法
  • 感谢您的回答,但我对 (mPhotoUri) 使用 Uri mPhotoUri 有疑问,将其更改为 String 时会出错。然后我将其替换为 (imagePath) 时会出现 RuntimeException 错误。
  • if(Build.VERSION.SDK_INT >= 19) 我使用的是 min sdk 9。
猜你喜欢
  • 1970-01-01
  • 2014-12-27
  • 2015-07-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-09
  • 2021-09-26
  • 1970-01-01
相关资源
最近更新 更多