Android Studio-带有ExifInterface的IOException-不存在此类文件或目录

时间:2018-10-22 11:53:57

标签: java android-studio gps exif

我正在使用Android Studio中的一个应用程序。该功能的一部分是访问缩略图的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'
    }

由于我遇到了这个问题,任何帮助将不胜感激。

干杯。

P.S。不知道这是否相关,但是我正在使用API​​ 28在模拟设备上测试我的应用。

0 个答案:

没有答案