当我尝试初始化此行中的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>
答案 0 :(得分: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。