Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/gwt/3.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 Intent_Camera - Fatal编程技术网

在Android中,如何在不保存图像文件的情况下获取图像

在Android中,如何在不保存图像文件的情况下获取图像,android,android-intent,camera,Android,Android Intent,Camera,提前感谢您的感谢 我正在下一节中编码。 a。使用内置摄像头应用程序(我使用Intent和其他应用程序拍摄pickture)。 b。在不保存到文件的情况下获取图像。 在我的应用程序中,用户选择一张信用卡并发送到服务器。信用卡图像文件不是必需的,将图像保存到文件中不利于安全 可能吗 如果这是不可能的,还有别的吗 a。打开jpg文件,将所有像素编辑为黑色 B使用 哪种方法合适?简短的回答是否 如果不将照片保存到图像中,则无法从默认照相机应用程序获取照片。 您可以使用Camera API在应用程序内拍照

提前感谢您的感谢

我正在下一节中编码。
a。使用内置摄像头应用程序(我使用Intent和其他应用程序拍摄pickture)。

b。在不保存到文件的情况下获取图像。

在我的应用程序中,用户选择一张信用卡并发送到服务器。信用卡图像文件不是必需的,将图像保存到文件中不利于安全

可能吗

如果这是不可能的,还有别的吗

a。打开jpg文件,将所有像素编辑为黑色
B使用


哪种方法合适?

简短的回答是

如果不将照片保存到图像中,则无法从默认照相机应用程序获取照片。
您可以使用Camera API在应用程序内拍照,而不是使用第三方默认系统照片应用程序。

查看此处

AndroidManifest.xml
上请求此权限:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
然后在一个
onClick
中触发此
Intent

Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (cameraIntent.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
        Log.i(TAG, "IOException");
    }
    // Continue only if the File was successfully created
    if (photoFile != null) {
        cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
        startActivityForResult(cameraIntent, REQUEST_IMAGE_CAPTURE);
    }
}
添加以下支持方法:

private File createImageFile() throws IOException {
    // Create an image file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    String imageFileName = "JPEG_" + timeStamp + "_";
    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
    mCurrentPhotoPath = "file:" + image.getAbsolutePath();
    return image;
}
然后接收结果:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
        try {
            mImageBitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), Uri.parse(mCurrentPhotoPath));
            mImageView.setImageBitmap(mImageBitmap);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

使它工作的是
MediaStore.Images.Media.getBitmap(this.getContentResolver(),Uri.parse(mCurrentPhotoPath))
,它与源代码不同。原始代码给了我一个
FileNotFoundException

您可以将其保存在临时文件中,然后在完成发送到服务器后将其删除。@SohailZahid谢谢您的帮助!!如何保存临时文件?@Sohail Zahid谢谢你的帮助!!如何保存在临时文件中?谢谢!我知道我必须使用摄像头API或擦除图像文件。
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
        try {
            mImageBitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), Uri.parse(mCurrentPhotoPath));
            mImageView.setImageBitmap(mImageBitmap);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}