0

当我尝试在这一行中初始化 exifinterface 时,我得到一个未处理的异常类型 IOException

            ExifInterface exif = new ExifInterface(imgFile.getCanonicalPath());

和这个 :

            exif.saveAttributes();

有人可以帮忙吗?这是我的代码,我正在尝试在 JPEG exif 中编写 gps 坐标。任何帮助将不胜感激!谢谢

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);
    Bitmap bm = (Bitmap) data.getExtras().get("data");
    iv.setImageBitmap(bm);
    SQLiteDatabase db1 = openOrCreateDatabase("names", MODE_PRIVATE, null);
    db1.execSQL("CREATE TABLE IF NOT EXISTS ExistentNames (name VARCHAR(25) PRIMARY KEY UNIQUE NOT NULL)");

    SQLiteDatabase db = openOrCreateDatabase("codes", MODE_PRIVATE, null);
    Cursor c = db.rawQuery("select * from ExistentCodes", null);
    c.moveToLast();
    String code = c.getString(c.getColumnIndex("code"));
    Intent intent = getIntent();
    Bundle extras = intent.getExtras();
    if (extras != null) {    
        createDirectoryAndSaveFile(bm, code+"_1.jpg");
     LocationManager mLocationManager =    (LocationManager)Capture.this.getSystemService(Context.LOCATION_SERVICE);
        rLocation = mLocationManager.getLastKnownLocation(LOCATION_SERVICE);
            File imgFile = new  File("/sdcard/Mouna/"+code+"_1.jpg");



        ExifInterface exif = new ExifInterface(imgFile.getCanonicalPath());

        //String latitudeStr = "90/1,12/1,30/1";
        double lat = rLocation.getLatitude();
        double alat = Math.abs(lat);
        String dms = Location.convert(alat, Location.FORMAT_SECONDS);
        String[] splits = dms.split(":");
        String[] secnds = (splits[2]).split("\\.");
        String seconds;
        if(secnds.length==0)
        {
            seconds = splits[2];
        }
        else
        {
            seconds = secnds[0];
        }

        String latitudeStr = splits[0] + "/1," + splits[1] + "/1," + seconds + "/1";
        exif.setAttribute(ExifInterface.TAG_GPS_LATITUDE, latitudeStr);

        exif.setAttribute(ExifInterface.TAG_GPS_LATITUDE_REF, lat>0?"N":"S");

        double lon = rLocation.getLongitude();
        double alon = Math.abs(lon);


        dms = Location.convert(alon, Location.FORMAT_SECONDS);
        splits = dms.split(":");
        secnds = (splits[2]).split("\\.");

        if(secnds.length==0)
        {
            seconds = splits[2];
        }
        else
        {
            seconds = secnds[0];
        }
        String longitudeStr = splits[0] + "/1," + splits[1] + "/1," + seconds + "/1";


        exif.setAttribute(ExifInterface.TAG_GPS_LONGITUDE, longitudeStr);
        exif.setAttribute(ExifInterface.TAG_GPS_LONGITUDE_REF, lon>0?"E":"W");


            exif.saveAttributes();


        ContentValues newData=new ContentValues();

        newData.put("name",code+"_1.jpg");

        db1.insert("ExistentNames",null,newData);}
else{
     Cursor d = db1.rawQuery("select * from ExistentNames", null);
        d.moveToLast();
        String name = d.getString(d.getColumnIndex("name"));
    createDirectoryAndSaveFile(bm, incrementname(name)+".jpg");
        //createDirectoryAndSaveFile(bm, name+"_2");
    ContentValues newData=new ContentValues();

    newData.put("name",incrementname(name)+".jpg");

    db1.insert("ExistentNames",null,newData);
} }

这是我的清单文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mouna.app"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="8" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
>

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" android:debuggable="true">
    <activity
        android:name="com.mouna.app.Main"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name="com.mouna.app.UserPage">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name="com.mouna.app.Capture"></activity>
    <activity
        android:name="com.mouna.app.Util"></activity>
</application>

4

1 回答 1

0

我会尝试更改创建文件名的代码,特别是这一行:

File imgFile = new  File("/sdcard/Mouna/"+code+"_1.jpg");

我不完全确定,但我怀疑它不能正确处理。这是我为创建 DiskCache 文件而复制的代码(不记得来自哪里),它似乎比您正在做的检查要多得多:

    String cacheFilePath;
    if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
            || !DiskCacheHelper.isExternalStorageRemovable()) {
        cacheFilePath = context.getExternalCacheDir().getPath();
    } else {
        cacheFilePath = context.getCacheDir().getPath();
    }

    File cacheFile = new File(cacheFilePath, "cachefile");

这是 getExternalCacheDir 函数:

public static boolean isExternalStorageRemovable() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
        return Environment.isExternalStorageRemovable();
    }
    return true;
}

现在,您可能没有为您的文件使用缓存目录(不清楚),但最好向系统查询正确的目录,而不是指定“/sdcard”。您可以进一步阅读Using External Storage

于 2013-11-12T21:45:17.003 回答