【问题标题】:Picture Intent Zero Length Image图片意图零长度图像
【发布时间】:2016-08-31 17:47:06
【问题描述】:

我在这里做错了什么?我正在尝试调用以获取全尺寸图片的意图:

takePictureIntent

private void takePictureIntent(int request) {
    final Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

    if (takePictureIntent.resolveActivity(activity.getPackageManager()) != null) {
        File file = null;

        try {
            file = createImageFile(request);
        } catch (Exception e) {
            showErrorDialog(getString(R.string.error), getString(R.string.error_saving_picture));
            Log.e(TAG, "Error while creating image file.");
        }

        if (file != null) {
            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
            startActivityForResult(takePictureIntent, request);
        } else {
            Log.e(TAG, "Error while creating image file.");
            showErrorDialog(getString(R.string.error), getString(R.string.error_saving_picture));
        }
    }
}

创建图像文件

private File createImageFile(final int request) {
    final File storageDir = new File(activity.getExternalFilesDir(Environment.DIRECTORY_PICTURES), getString(R.string.app_name));

    if (!storageDir.exists()) {
        if (!storageDir.mkdirs()) {
            Log.e(TAG, "Cannot create parent folders.");
            return null;
        }
    }

    File file = null;

    try {
        file = File.createTempFile("test_", ".jpg", storageDir);
    } catch (Exception e) {
        Log.e(TAG, "Error while creating temp file.");
    }

    fileProduct = file;

    return file;
}

onActivityResult

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == Activity.RESULT_OK && requestCode == REQUEST_IMAGE_PRODUCT) {

    if (fileProduct == null ||!fileProduct.exists() ||fileProduct.length() == 0) {
        showErrorDialog(getString(R.string.error), getString(R.string.error_taking_product_picture));
        return;
    }
}

有时(是的,有时)生成的文件的长度为 0。我确信私有应用程序上下文中的文件夹存在并且图像文件也存在(长度 > 0)。你能提供一些帮助吗?我在 Nexus 5X 上使用 6.0。

【问题讨论】:

    标签: android android-intent onactivityresult android-photos


    【解决方案1】:

    我会先摆脱File.createTempFile()。您不需要它,这会浪费时间,并且可能会导致某些相机应用不想将照片存储在该文件中(因为该文件已经存在)。只需自己生成一个唯一的文件名。这将逐步提高您的兼容性。

    另外,您需要确保在保存的实例状态Bundle 中保持fileProduct,因为当相机应用程序处于前台时,您的应用程序的进程可能会终止。

    但是,总的来说,ACTION_IMAGE_CAPTURE 不是很可靠。您将这项工作委托给数百个可能的相机应用程序之一,其中一些应用程序存在错误。一个这样的错误是忽略EXTRA_OUTPUT。因此,在onActivityResult() 中,如果您确定您有一个有效的fileProduct 值,但那里没有文件,请调用data.getData() 并查看那里是否有Uri。在这种情况下,相机应用程序可能已将照片存储在该Uri 标识的位置,您可以使用ContentResolverDocumentFile 尝试使用该Uri

    【讨论】:

    • 1 和 2 还可以,但我不希望 Nexus 相机应用程序出现这样的错误...此代码在带有 Lollipop 的摩托罗拉 Moto G 2014 上运行良好。如果这可能有帮助,我将从片段中调用 startActivityForResult。我没有得到这里,是图像在那里,文件系统有它......呜咽。
    • @Jumpa:那么你的问题不是零长度文件。我的猜测是fileProductnull,因为你的进程在后台被终止了。
    • 我测试了文件存在(我的意思是方法exists())并且不为空。
    【解决方案2】:

    使用这个:

    final String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    file = new File(storageDir.getPath() + File.separator + "IMG_" + timeStamp + ".jpg");
    

    而不是

    File.createTempFile()
    

    似乎神奇地解决了这个问题。感谢 CommonsWare(以某种方式)为我指明了正确的方向。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多