我正在尝试从图库中选取图像并使用ExifInterface打印它的路径以及gps标记。我正在使用nexus 4(kitkat 4.4.2)进行调试,我得到的图像路径是
/存储/模拟/ 0 / DCIM /映像文件名 但是ExifInterface为所有标签返回null,并且logcat显示错误JHEAD无法打开'/ storage / emulated / 0 / DCIM / image_file_name'(应用程序不会崩溃)。 请帮忙。
主要活动代码:
package com.example.images;
import java.io.File;
import java.io.IOException;
import android.media.ExifInterface;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
startActivityForResult(intent,0);
}
});
}
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (resultCode == RESULT_OK) {
ImageView iv= (ImageView)findViewById(R.id.imageView1);
Uri uri = intent.getData();
RelativeLayout rl = (RelativeLayout) findViewById(R.id.rl);
Toast.makeText(rl.getContext(), uri.getPath(), Toast.LENGTH_SHORT).show();
iv.setImageURI(uri);
String s = getPath(uri);
TextView tv= new TextView(rl.getContext());
//System.out.println(s);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.RIGHT_OF, R.id.imageView1);
tv.setLayoutParams(params);
rl.addView(tv);
tv.setText(s);
tv.setId(1);
//File f= new File(s);
//Toast.makeText(rl.getContext(), f.getName(), Toast.LENGTH_SHORT).show();
try {
ExifInterface exif = new ExifInterface(s);
String myAttribute="Exif information ---\n";
myAttribute += getTagString(ExifInterface.TAG_GPS_LATITUDE, exif);
myAttribute += getTagString(ExifInterface.TAG_GPS_LATITUDE_REF, exif);
myAttribute += getTagString(ExifInterface.TAG_GPS_LONGITUDE, exif);
myAttribute += getTagString(ExifInterface.TAG_GPS_LONGITUDE_REF, exif);
float[] output = null;
Boolean x= exif.getLatLong(output);
System.out.print(x);
TextView tv1= new TextView(rl.getContext());
RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params1.addRule(RelativeLayout.RIGHT_OF, R.id.imageView1);
params1.addRule(RelativeLayout.BELOW, 1);
tv1.setLayoutParams(params1);
rl.addView(tv1);
tv1.setText(myAttribute);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
private String getTagString(String tag, ExifInterface exif)
{
return(tag + " : " + exif.getAttribute(tag) + "\n");
}
@SuppressWarnings("deprecation")
public String getPath(Uri uri) {
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(uri, projection, null, null, null);
if(cursor!=null)
{
//HERE YOU WILL GET A NULLPOINTER IF CURSOR IS NULL
//THIS CAN BE, IF YOU USED OI FILE MANAGER FOR PICKING THE MEDIA
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
if(cursor.getString(column_index) != null)
return cursor.getString(column_index);
else return uri.getPath();
}
else return uri.getPath();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
logcat:
02-18 19:59:04.267: D/dalvikvm(9815): Late-enabling CheckJNI
02-18 19:59:04.287: D/dalvikvm(9815): Debugger has detached; object registry had 1 entries
02-18 19:59:04.538: D/OpenGLRenderer(9815): Enabling debug mode 0
02-18 19:59:11.905: D/dalvikvm(9815): GC_FOR_ALLOC freed 96K, 2% free 9147K/9280K, paused 15ms, total 16ms
02-18 19:59:11.946: I/dalvikvm-heap(9815): Grow heap (frag case) to 39.442MB for 31961104-byte allocation
02-18 19:59:11.956: D/dalvikvm(9815): GC_FOR_ALLOC freed 3K, 1% free 40356K/40496K, paused 13ms, total 13ms
02-18 19:59:12.306: E/JHEAD(9815): can't open '/storage/emulated/0/DCIM/Camera/IMG_20140217_210408.jpg'
清单文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.images"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="9"
android:targetSdkVersion="14" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.images.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>