Android Studio - IOException с ExifInterface - такого файла или каталога не существует - PullRequest
0 голосов
/ 22 октября 2018

Я работаю над приложением в Android-студии.Частью функциональности является доступ к GPS-координатам миниатюрного изображения.Я пытаюсь сделать это через ExifInterface, но продолжаю получать сообщение об ошибке, когда я пытаюсь создать экземпляр интерфейса с путем эскиза:

2018-10-22 22: 12: 55.684 ####- #### / имя_пакета E / имя_ действия : ExifInterface: содержимое IOException: / media / external / images / media / (Нет такого файла или каталога)

Я включил соответствующий код ниже:

    String photoUri = getImageUri(CreateEvent.this, eventThumbnail);
    if (photoUri != null) {
        imageLatLng = getImageGPSTag(photoUri);
        if (imageLatLng != null) {
            latitude = String.valueOf(imageLatLng.latitude);
            longitude = String.valueOf(imageLatLng.longitude);
            Log.d(TAG, "createEvent: got exif gps tag coords");
        }
    }

    // method to get uri of thumbnail
    private String getImageUri(Context context, Bitmap inImage) {

        if (ContextCompat.checkSelfPermission(CreateEvent.this,
            Manifest.permission.WRITE_EXTERNAL_STORAGE) != 
            PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(CreateEvent.this,
                new String[] {Manifest.permission.WRITE_EXTERNAL_STORAGE},
                WRITE_EXTERNAL_STORAGE_REQUEST_CODE);
        }

        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
        String path = MediaStore.Images.Media.insertImage(CreateEvent.this.getContentResolver(),
            inImage, "New Event", null);
        Log.d(TAG, "image uri path is : " + path);
        File imageDir = new File(path);
        if (!imageDir.exists()) {
            imageDir.mkdirs();
        }
        return path;
    }

    //method to get gps coords of image using exifinterface
    private LatLng getImageGPSTag(String imageUri) {
        LatLng latLng;

        try {
            ExifInterface exifInterface = new ExifInterface(imageUri);
            String latitude = exifInterface.getAttribute(ExifInterface.TAG_GPS_LATITUDE);
            String longitude = exifInterface.getAttribute(ExifInterface.TAG_GPS_LONGITUDE);
            latLng = new LatLng(Float.parseFloat(latitude), Float.parseFloat(longitude));
        } catch (IOException e) {
            Log.e(TAG, "ExifInterface: IOException " + e.getMessage());
            latLng = null;
        }
        return latLng;
    }

На всякий случай, если произошла ошибка с моим файлом build.gradle приложения, я также включил его:

    apply plugin: 'com.android.application'
    apply plugin: 'com.google.gms.google-services'
    android {
        compileSdkVersion 28
        defaultConfig {
            applicationId "******"
            minSdkVersion 26
            targetSdkVersion 28
            versionCode 1
            versionName "1.0"
            testInstrumentationRunner 
    "android.support.test.runner.AndroidJUnitRunner"
        }
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            }
        }
    }
    dependencies {
        implementation fileTree(dir: 'libs', include: ['*.jar'])
        implementation 'com.android.support:appcompat-v7:28.0.0'
        implementation 'com.android.support:design:28.0.0'
        implementation 'com.android.support:exifinterface:28.0.0'
        implementation 'com.google.firebase:firebase-auth:16.0.4'
        implementation 'com.google.firebase:firebase-core:16.0.4'
        implementation 'com.google.firebase:firebase-messaging:17.3.3'
        implementation 'com.google.firebase:firebase-database:16.0.3'
        implementation 'com.google.firebase:firebase-storage:16.0.3'
        implementation 'com.google.android.gms:play-services-analytics:16.0.4'
        implementation 'com.google.android.gms:play-services-auth:16.0.1'
        implementation 'com.google.android.gms:play-services-gcm:16.0.0'
        implementation 'com.google.android.gms:play-services-identity:16.0.0'
        implementation 'com.google.android.gms:play-services-location:16.0.0'
        implementation 'com.google.android.gms:play-services-maps:16.0.0'
        implementation 'com.google.android.gms:play-services-places:16.0.0'
        implementation 'com.android.support.constraint:constraint-layout:1.1.3'
        implementation 'android.arch.lifecycle:extensions:1.1.1'
        testImplementation 'junit:junit:4.12'
        androidTestImplementation 'com.android.support.test:runner:1.0.2'
        androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    }

Любая помощь будет принята с благодарностью, поскольку я столкнулся с этой проблемой.

Приветствия.

PS Не уверен, что это актуально, но я проверяю своюприложение на эмулируемом устройстве с API 28.

...