在KitKat上捕获后,Android Camera强制关闭

时间:2017-01-27 11:10:11

标签: android-4.4-kitkat android-camera-intent forceclose

目前我正在创建允许用户拍照并将其显示到ImageView的应用。它的效果就像Android 5.1.1 Sony M2 Dual上的魅力一样。 但是在Kitkat 4.4.2三星Galaxy Tab 3和KitKat 4.4.4小米Redmi 2上,拍摄图像后相机强制关闭。

我不知道这是否有用,但我意识到相机可能会强制关闭,因为捕获后,在这两个KitKat设备上,用户将被提示接受捕获的图片,然后如果被接受,它将返回到我当前的活动。

由于我的索尼5.1.1,用户不会被提示输入捕获的图片,它将直接返回我当前的活动。

这里我包含了相应的代码。

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_job_order_line_form);

    bAddPhoto = (TextView) findViewById(R.id.bAddPhoto);
    bSaveJOLine = (TextView) findViewById(R.id.bSaveJOLine);

    editDescription = (EditText) findViewById(R.id.editDescription);
    editQty = (EditText) findViewById(R.id.editQty);
    editPrice = (EditText) findViewById(R.id.editPrice);
    ivImage = (ImageView) findViewById(R.id.imageTaken);

    Intent intent = getIntent();
    jobId = intent.getIntExtra("jobId", 0);
    docNo = intent.getStringExtra("docNo");

    bAddPhoto.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intentCamera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            if (intentCamera.resolveActivity(getPackageManager()) != null) {
                // Create the File where the photo should go
                File photoFile = null;
                try {
                    photoFile = createImageFile();
                } catch (IOException ex) {
                    // Error occurred while creating the File
                    Toast.makeText(OJobOrderLineFormActivity.this, "Create file failed!",
                            Toast.LENGTH_SHORT).show();
                }
                // Continue only if the File was successfully created
                if (photoFile != null) {
                    Uri photoURI = FileProvider.getUriForFile(OJobOrderLineFormActivity.this,
                            "com.opentoko.opentokolaundry.fileprovider",
                            photoFile);
                    System.out.println(photoURI);
                    intentCamera.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
                    startActivityForResult(intentCamera, 360);
                }
            }
        }
    });

    bSaveJOLine.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Snackbar.make(v, "Item has been saved!", Snackbar.LENGTH_LONG)
                    .setAction("OK", null).show();
        }
    });

}

创建临时文件功能:

    private File createImageFile() throws IOException {
    // Create an image file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmm").format(new Date());
    String imageFileName = docNo + "_" + timeStamp + "_";
    File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
    File image = File.createTempFile(
            imageFileName,  /* prefix */
            ".jpg",         /* suffix */
            storageDir      /* directory */
    );
    System.out.println(storageDir);
    System.out.println(image);

    // Save a file: path for use with ACTION_VIEW intents
    currentPhotoPath = image.getAbsolutePath();
    System.out.println(currentPhotoPath);

    return image;
}

onActivityResult:

    @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    switch (requestCode) {
        case 360:
            if(resultCode == RESULT_OK) {
                int targetW = ivImage.getWidth();
                int targetH = ivImage.getHeight();

                // Get the dimensions of the bitmap
                BitmapFactory.Options bmOptions = new BitmapFactory.Options();
                bmOptions.inJustDecodeBounds = true;
                BitmapFactory.decodeFile(currentPhotoPath, bmOptions);
                int photoW = bmOptions.outWidth;
                int photoH = bmOptions.outHeight;

                // Determine how much to scale down the image
                int scaleFactor = Math.min(photoW/targetW, photoH/targetH);

                // Decode the image file into a Bitmap sized to fill the View
                bmOptions.inJustDecodeBounds = false;
                bmOptions.inSampleSize = scaleFactor;
                bmOptions.inPurgeable = true;

                Bitmap bitmap = BitmapFactory.decodeFile(currentPhotoPath, bmOptions);
                ivImage.setImageBitmap(bitmap);
            }
    }
}

我无法弄清楚导致这种情况的原因,因为在logcat中似乎没有任何错误,我的构建仍然像往常一样运行。 这是我输入当前活动后的logcat拍照:

D/TextLayoutCache: Enable myanmar Zawgyi converter
D/TextLayoutCache: Enable myanmar Zawgyi converter
D/TextLayoutCache: Enable myanmar Zawgyi converter
D/TextLayoutCache: Enable myanmar Zawgyi converter
I/System.out: /storage/emulated/0/Android/data/com.opentoko.opentokolaundry/files/Pictures
I/System.out: /storage/emulated/0/Android/data/com.opentoko.opentokolaundry/files/Pictures/A-00003_20170127_1758_-1345208956.jpg
I/System.out: /storage/emulated/0/Android/data/com.opentoko.opentokolaundry/files/Pictures/A-00003_20170127_1758_-1345208956.jpg
I/System.out: content://com.opentoko.opentokolaundry.fileprovider/my_images/A-00003_20170127_1758_-1345208956.jpg
W/IInputConnectionWrapper: showStatusIcon on inactive InputConnection

这个logcat已经在这个确切的点开始时开始打开相机。捕获后,没有任何东西添加到logcat。

这是我的Android-Manifest.xml的一部分:

    <uses-feature android:name="android.hardware.camera" android:required="true" />

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

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <provider
        android:name="android.support.v4.content.FileProvider"
        android:authorities="com.opentoko.opentokolaundry.fileprovider"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/file_paths">
        </meta-data>
    </provider>

这里有没有人或有同样的问题?有解决方案吗任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

我通过反复试验找到了针对KitKat关闭意图相机力的解决方案。 似乎我根本不需要FileProvider。

我将createImageFile()中的storageDir更改为:

Uri photoURI = Uri.fromFile(photoFile);

和photoURI到此:

private CharsetDecoder decoder = Charset.forName("UTF-8").newDecoder();
decoder.decode(record.getData()).toString();

现在我也有正确的全屏图像显示在KitKat上。

我从清单中删除提供程序。现在我将我捕获的图像文件放在Android / data / my.package.name / files / Pictures /文件夹中。