【问题标题】:Exif Information Rotation getting file not found exceptionExif 信息轮换获取文件未找到异常
【发布时间】:2018-01-26 18:45:17
【问题描述】:

我一直试图弄清楚 EXIF 有一段时间,但我只是一个又一个问题,一个又一个问题。我希望有人可以为我阐明这一点。

我有一个用户按下的按钮,它执行此意图

Intent intent = new Intent();
// Show only images, no videos or anything else
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
// Always show the chooser (if there are multiple options available)
startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);

PICK_IMAGE_REQUEST如下

private int PICK_IMAGE_REQUEST = 1;

这是我的 OnActivityResult。

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {

        mMediaUri = data.getData();

        try {
            Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), mMediaUri);

            String fileName2 = FileHelper.getFileName(UserProfileActivity.this, mMediaUri, "image");
            try {
                ExifInterface exifInterface = new ExifInterface(fileName2);
                int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1);
                System.out.println(orientation + "---------------------------");
                Matrix matrix = new Matrix();
                if (orientation == 6) {
                    System.out.println("oritation 6, rotate 90");
                    matrix.postRotate(90);
                } else if (orientation == 3) {
                    System.out.println("oritation 3, rotate 180");
                    matrix.postRotate(180);
                } else  if (orientation == 8) {
                    System.out.println("oritation 8, rotate 270");
                    matrix.postRotate(270);
                }
                bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }

            //friendsProfilePic imageView = (ImageView) findViewById(R.id.imageView);
            if (userChangedImage){
                userProfileImage.setImageBitmap(bitmap);
            }
            userProfileImageEditProfile.setImageBitmap(bitmap);

            final ParseQuery<ParseUser> queryUser = ParseUser.getQuery();
            try {
                byte[] fileBytes = FileHelper.getByteArrayFromFile(UserProfileActivity.this, mMediaUri);
                if (fileBytes == null) {
                    //there was an error
                    Toast.makeText(getApplicationContext(), "There was an error. Try again!", Toast.LENGTH_LONG).show();
                } else {
                    fileBytes = FileHelper.reduceImageForUpload(fileBytes);
                    String fileName = FileHelper.getFileName(UserProfileActivity.this, mMediaUri, "image");
                    userChangedImageFile = new ParseFile(fileName, fileBytes);
                    userChangedImage = true;
                }
            } catch (Exception e1) {
                e1.printStackTrace();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

最后,这就是控制台内部正在读取的内容。

System.out: image.png (No such file or directory)

对此的任何帮助将不胜感激! 谢谢

【问题讨论】:

  • 你应该调试你的代码,看看 data.getData() 的实际样子。

标签: android exif


【解决方案1】:

FileHelper.getFileName() 几乎肯定是错误的,因为Uri 不需要代表文件。我强烈建议您完全摆脱它。

使用ContentResolver(来自Activity 上的getContentResolver())和openInputStream()Uri 标识的内容上获得InputStream。您可以将其传递给a constructor on the support library edition of ExifInterface 以尝试获取方向标记(如果存在)。

【讨论】:

    【解决方案2】:

    您应该使用兼容的 ExifInterface。

    com.android.support:exifinterface:${lastLibVersion}

    您将能够使用 InputStream(来自 ContentResolver)而不是 uri 路径来实例化 ExifInterface(pior API

    https://android-developers.googleblog.com/2016/12/introducing-the-exifinterface-support-library.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-21
      • 1970-01-01
      • 2020-10-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多