Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/220.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 在小米红米设备中捕获文件时显示空图像_Android_Android Gallery_Android Camera Intent - Fatal编程技术网

Android 在小米红米设备中捕获文件时显示空图像

Android 在小米红米设备中捕获文件时显示空图像,android,android-gallery,android-camera-intent,Android,Android Gallery,Android Camera Intent,我想为捕获图像添加代码,并从库中选择图像。我添加了它,并在一些设备上工作,如摩托罗拉和三星。但当我在小米红米上测试时,它在拍摄图像时崩溃了,文件名返回null,而从gallery中选择图像时,它没有被选中,光标返回null private void selectImage() { final CharSequence[] items = { "Take Photo", "Choose from Library", "Cancel" }; AlertD

我想为捕获图像添加代码,并从库中选择图像。我添加了它,并在一些设备上工作,如摩托罗拉和三星。但当我在小米红米上测试时,它在拍摄图像时崩溃了,文件名返回null,而从gallery中选择图像时,它没有被选中,光标返回null

  private void selectImage() {
    final CharSequence[] items = { "Take Photo", "Choose from Library",
            "Cancel" };

    AlertDialog.Builder builder = new AlertDialog.Builder(RegisterActivity.this);
    builder.setTitle("Add Photo!");
    builder.setItems(items, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int item) {


                if (items[item].equals("Take Photo")) {
                    userChoosenTask = "Take Photo";

                    result = Utility.checkAndRequestPermissions(RegisterActivity.this);

                    if (result) {
                        cameraIntent();
                    }

                } else if (items[item].equals("Choose from Library")) {
                    userChoosenTask = "Choose from Library";

                    result = Utility.checkAndRequestPermissions(RegisterActivity.this);

                    if (result) {
                        galleryIntent();
                    }

                } else if (items[item].equals("Cancel")) {
                    dialog.dismiss();
                }

        }
    });
    builder.show();

}


  private void galleryIntent()
{

    Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

    startActivityForResult(intent,SELECT_FILE);

}

private void cameraIntent()
{
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
   if (intent.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
         //   UiUtils.showAlert(getString(R.string.error),NewGroupAcvitity.this);
        }
        // Continue only if the File was successfully created
        if (photoFile != null) {
           intent.putExtra(MediaStore.EXTRA_OUTPUT,
                    Uri.fromFile(photoFile));
            startActivityForResult(intent,REQUEST_CAMERA);
        }
    }

}
private File createImageFile() throws IOException {
    // Create an image file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    String imageFileName = "image";
    File storageDir = Environment.getExternalStoragePublicDirectory(
            Environment.DIRECTORY_PICTURES);
    File image = File.createTempFile(
            imageFileName,  /* prefix */
            ".jpg",         /* suffix */
            storageDir      /* directory */
    );

    // Save a file: path for use with ACTION_VIEW intents
    fileName =  image.getAbsolutePath();
    return image;
}

public void loadImageFromFile(String imageFile){

    try {
        ExifInterface ei = new ExifInterface(imageFile);
        int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION,
                ExifInterface.ORIENTATION_UNDEFINED);

        Bitmap bitmap = BitmapFactory.decodeFile(imageFile);

        Bitmap rotatedBitmap = null;

        switch (orientation) {
            case ExifInterface.ORIENTATION_ROTATE_90:
                rotatedBitmap = rotateImage(bitmap, 90);
                break;
            case ExifInterface.ORIENTATION_ROTATE_180:
                rotatedBitmap = rotateImage(bitmap, 180);
                break;
            case ExifInterface.ORIENTATION_ROTATE_270:
                rotatedBitmap = rotateImage(bitmap, 270);
                break;
            case ExifInterface.ORIENTATION_NORMAL:
            default:
                rotatedBitmap = bitmap;
                break;
        }

        if(rotatedBitmap != null)
        {
            profile_image.setImageBitmap(rotatedBitmap);
            selectedBitmap = rotatedBitmap;

            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            selectedBitmap.compress(Bitmap.CompressFormat.PNG, 100, stream); //replace 100 with desired quality percentage.
            byte[] byteArray = stream.toByteArray();

                File tempFile = File.createTempFile("temp",null, getCacheDir());
                FileOutputStream fos = new FileOutputStream(tempFile);
                fos.write(byteArray);

                mProfileImage = tempFile;
        }

    }
    catch (IOException ex) {
      //  UiUtils.showAlert(getString(R.string.error),NewGroupAcvitity.this);
    }
}

public static Bitmap rotateImage(Bitmap source, float angle) {
    Matrix matrix = new Matrix();
    matrix.postRotate(angle);
    return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix,
            true);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (resultCode == Activity.RESULT_OK) {
        if (requestCode == SELECT_FILE)
            onSelectFromGalleryResult(data);
        else if (requestCode == REQUEST_CAMERA)
            onCaptureImageResult(data);
    }
}

private void onCaptureImageResult(Intent data) {

    loadImageFromFile(fileName);

}

@SuppressWarnings("deprecation")
private void onSelectFromGalleryResult(Intent data) {

    Uri uri = (Uri)data.getData();

    Log.d("data", String.valueOf(data));

    String[] filePathColumn = { MediaStore.Images.Media.DATA };
    Cursor cursor = getContentResolver().query(uri,filePathColumn, null, null, null);
    if(cursor != null) {
        cursor.moveToFirst();
        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        String picturePath = cursor.getString(columnIndex);
        cursor.close();
        loadImageFromFile(picturePath);
    }
}
另外,选择或捕获图像需要花费太多时间超过10秒,原因是什么


有人能帮忙吗?谢谢..

文件名返回null-当应用程序处于后台时,您的进程可能已终止。确保您通过保存的实例状态包保留文件名。您能举个例子吗@Commonsware为什么这需要更长的时间来捕获或选择图像?有大约20亿台Android设备,来自数千种设备型号。这些设备型号中预装了数百个摄像头应用程序,用户可以安装其他摄像头应用程序。您的代码将拍照委托给用户选择使用的任何一个摄像头应用程序。摄像头应用程序的行为取决于摄像头应用程序的开发人员,而不是您。如果你不喜欢小米摄像头应用程序,请与该应用程序的开发人员联系。