【问题标题】:Extract exif information of image in android提取android中图片的exif信息
【发布时间】:2015-05-17 02:30:03
【问题描述】:

我是 android 开发新手,正在尝试使用 ExifInterface 获取图像的元数据。我将图像存储在可绘制对象下,并尝试获取元数据,但获取所有字段(日期、图像长度、图像宽度)的空值。我试图这样访问图像路径:

          String path = "drawable://" + R.drawable.testimage;

并提供了 ExifInterface 的路径。

          ExifInterface exif = new ExifInterface(path);

我不知道在drawable下存储图像是否正确,因为当我在模拟器中运行应用程序时,我会得到这样的结果:

          E/JHEAD﹕ can't open 'drawable://2130837561'

所以如果这是错误的,那么请告诉我应该将图像存储在哪里以及如何提供 ExifInterface 的图像路径。 提前谢谢你。

【问题讨论】:

  • 我不明白,在创建你的apk文件的过程中,drawable文件夹中的图像被优化(通过压缩它们)。此外,所有 Exif 数据都将被删除,以节省空间。那么如果 Exif 数据不存在,那么读取它有什么意义呢?

标签: android image exif


【解决方案1】:

要获得一个drawable,你可以用这个sn-p:

  Drawable drawable = getResources().getDrawable(android.R.drawable.your_drawable);

我不确定你的方式是否正确,因为我从未见过这样的方式。您真的需要图像的路径才能在 ExifInterface 类上使用它吗?

好的,我进行了一些挖掘并找到了this question,这使我找到了this one。看起来,您可以从您的 apk 中的资源获取绝对路径。一个好的解决方案是你将它作为文件保存在外部存储器上,然后你就可以得到你想要的路径。

首先,将此添加到您的 AndroidManifest.xml 中,以便您的应用可以写入手机内存:

<uses-permission
 android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

好的,要保存它,你可以试试这个,首先从你的可绘制资源创建一个位图:

Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.your_drawable);

之后获取你要保存图片的路径,并将其放在String 上。有关here 的更多信息。

Android 文档有一个很好的例子来说明如何获取路径。你可以看到它here

为简单起见,我将从文档中复制并粘贴 sn-p。

    void createExternalStoragePrivateFile() {
    // Create a path where we will place our private file on external
    // storage.
    File file = new File(getExternalFilesDir(null), "DemoFile.jpg");

    try {
        // Very simple code to copy a picture from the application's
        // resource into the external file.  Note that this code does
        // no error checking, and assumes the picture is small (does not
        // try to copy it in chunks).  Note that if external storage is
        // not currently mounted this will silently fail.
        InputStream is = getResources().openRawResource(R.drawable.balloons);
        OutputStream os = new FileOutputStream(file);
        byte[] data = new byte[is.available()];
        is.read(data);
        os.write(data);
        is.close();
        os.close();
    } catch (IOException e) {
        // Unable to create file, likely because external storage is
        // not currently mounted.
        Log.w("ExternalStorage", "Error writing " + file, e);
    }
}

void deleteExternalStoragePrivateFile() {
    // Get path for the file on external storage.  If external
    // storage is not currently mounted this will fail.
    File file = new File(getExternalFilesDir(null), "DemoFile.jpg");
    if (file != null) {
        file.delete();
    }
}

boolean hasExternalStoragePrivateFile() {
    // Get path for the file on external storage.  If external
    // storage is not currently mounted this will fail.
    File file = new File(getExternalFilesDir(null), "DemoFile.jpg");
    if (file != null) {
        return file.exists();
    }
    return false;
}

然后,获取你保存在外部存储器上的文件的路径,然后做你想做的。

我也会保留旧示例。可以使用getExternalStorageDirectory()方法获取路径,或者getExternalCacheDir()。之后,您可以使用名为getAbsolutePath()File 方法来获取您的String

String path = (...) // (you can choose where to save here.)
File file = new File(path, "your_drawable.png");
FileOutputStream out = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, out); // You can change the quality from 0 to 100 here, and the format of the file. It can be PNG, JPEG or WEBP. 
out.flush();
out.close();

有关 Bitmap 类的更多信息,请查看the docs

如果您需要更多信息,请告诉我,我会尝试展示更多示例。

编辑:我看到了你的链接,那里有这个sn-p:

    //change with the filename & location of your photo file
        String filename = "/sdcard/DSC_3509.JPG";
        try {
   ExifInterface exif = new ExifInterface(filename);
   ShowExif(exif);
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
   Toast.makeText(this, "Error!", 
     Toast.LENGTH_LONG).show();
  }

如您所见,如果您真的想查看内部图像资源的exif数据,则必须将其保存在其他地方,然后您可以尝试获取该文件的绝对路径,然后调用显示exif的方法。

【讨论】:

  • 感谢@Mauker 的回复。您说好的解决方案是将其保存在外部存储器中。你能解释一下吗?我只在计算机(模拟器)上测试我的应用程序。那么如何将图像存储到外部存储器并获取其路径?
  • 对不起@Mauker 我没明白。请用更简单的方式告诉我。 1)我将图像存储在drawable下。 2)如何获取文件(图像)名称,因为我必须向 ExifInterface 提供文件名。请在这里举一个例子,我指的是link。如本例所示,如何为 ExifInterface 提供文件名。
  • 您的链接有问题,它说该页面不存在...但是好的,我会尝试用更简单的方式解释。
  • 嗯,我现在有点困,睡一晚后我可以改善答案。所以...您可以尝试使用我提供的示例和链接: 1- 将您的资源作为图像保存在外部存储器上。 2-获取该文件的路径。 3-使用路径,提取exif数据。
  • 谢谢。我感谢您的帮助。很高兴告诉你我终于能够解决这个问题。再次感谢您。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-18
  • 2015-07-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多