【问题标题】:Image file is empty on return from activity on Android从 Android 上的活动返回时,图像文件为空
【发布时间】:2016-06-29 04:36:21
【问题描述】:

使用此答案中的代码:https://stackoverflow.com/a/12347567/734687

我使用以下代码让用户向我的 Android 应用程序提交图片。

private void openImageIntent() {
    File outputFile = null;
    try {
        outputFile = File.createTempFile("tmp", "face", getCacheDir());
    } catch (IOException pE) {
        pE.printStackTrace();
    }
    mOutputFileUri = Uri.fromFile(outputFile);

    // Camera.
    final List<Intent> cameraIntents = new ArrayList<Intent>();
    final Intent captureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    final PackageManager packageManager = getPackageManager();
    final List<ResolveInfo> listCam = packageManager.queryIntentActivities(captureIntent, 0);
    for(ResolveInfo res : listCam) {
        final String packageName = res.activityInfo.packageName;
        final Intent intent = new Intent(captureIntent);
        intent.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
        intent.setPackage(packageName);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, mOutputFileUri);
        cameraIntents.add(intent);
    }

    // Filesystem.
    final Intent galleryIntent = new Intent();
    galleryIntent.setType("image/*");
    galleryIntent.setAction(Intent.ACTION_PICK);

    // Chooser of filesystem options.
    final Intent chooserIntent = Intent.createChooser(galleryIntent, "Select Source");

    // Add the camera options.
    chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, cameraIntents.toArray(new Parcelable[cameraIntents.size()]));

    startActivityForResult(chooserIntent, 42); //XXX 42?
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK) {
        if (requestCode == 42) {
            final boolean isCamera;
            if (data == null) {
                isCamera = true;
            } else {
                final String action = data.getAction();
                if (action == null) {
                    isCamera = false;
                } else {
                    isCamera = action.equals(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                }
            }
            Uri selectedImageUri = null;
            if (isCamera) {
                selectedImageUri = mOutputFileUri;
            } else {
                selectedImageUri = data == null ? null : data.getData();
            }
            loadFacePicture(selectedImageUri);
        }
    }
}

但是 Uri selectedImageUri 导致 loadFacePicture(selectedImageUri); 上的空文件

我错过了什么?

【问题讨论】:

  • String action = data.getAction(); 有什么价值?
  • @ρяσѕρєяK 实际上数据为空。 resultCode 是 RESULT_OK 和 requestCode 42 tho
  • 我建议你检查这个答案:stackoverflow.com/a/36394744/361100
  • @Youngjae 在我的情况下,我没有数据和无效的 URI...听起来有点像死胡同...
  • @Antzi // 删除if (resultCode == RESULT_OK) 怎么样?我测试了你的代码,Gallerray 应用程序很好地提供了 uri。但默认相机应用程序在我从相机捕获后将 resultCode 提供为RESULT_CANCELED。

标签: android android-6.0-marshmallow


【解决方案1】:

这就是我最终要做的。 这似乎是问题的根源:

    intent.putExtra(MediaStore.EXTRA_OUTPUT, mOutputFileUri);

实际代码:

private void openImageIntent() {
        try {
            outputFile = File.createTempFile("tmp", ".jpg", getCacheDir());
        } catch (IOException pE) {
            pE.printStackTrace();
        }
        mOutputFileUri = Uri.fromFile(outputFile);

        // Camera.
        final List<Intent> cameraIntents = new ArrayList<Intent>();
        final Intent captureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        final PackageManager packageManager = getPackageManager();
        final List<ResolveInfo> listCam = packageManager.queryIntentActivities(captureIntent, 0);
        for(ResolveInfo res : listCam) {
            final String packageName = res.activityInfo.packageName;
            final Intent intent = new Intent(captureIntent);
            intent.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
            intent.setPackage(packageName);
            cameraIntents.add(intent);
        }

        // Filesystem.
        final Intent galleryIntent = new Intent();
        galleryIntent.setType("image/*");
        galleryIntent.setAction(Intent.ACTION_PICK);

        // Chooser of filesystem options.
        final Intent chooserIntent = Intent.createChooser(galleryIntent, "Select Source");

        // Add the camera options.
        chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, cameraIntents.toArray(new Parcelable[cameraIntents.size()]));

        startActivityForResult(chooserIntent, 42);
    }


@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == 42) {
                Bitmap bmp = null;
                if (data.hasExtra("data")) {
                    Bundle extras = data.getExtras();
                    bmp = (Bitmap) extras.get("data");
                } else {
                    AssetFileDescriptor fd = null;
                    try {
                        fd = getContentResolver().openAssetFileDescriptor(data.getData(), "r");
                    } catch (FileNotFoundException pE) {
                        pE.printStackTrace();
                    }
                    bmp = BitmapFactory.decodeFileDescriptor(fd.getFileDescriptor());
                }
                try {
                        FileOutputStream out = new FileOutputStream(new File(mOutputFileUri.getPath()));
                        bmp.compress(Bitmap.CompressFormat.JPEG, 100, out);
                        out.flush();
                        out.close();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                loadFacePicture(mOutputFileUri);
            }
        }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-26
    • 2016-05-14
    • 1970-01-01
    • 2011-05-01
    • 2014-10-11
    相关资源
    最近更新 更多