猿问

如何将图像文件从相机添加到我创建的路径文件

好的,我一直在寻找解决此问题的方法,我从相机或图库中捕获图像并创建了图像文件路径但不幸的是我可以获得图像,因为来自相机或图库的图像不在创建的文件路径中实际上。那么我该如何解决呢?


我真的不知道在这里说什么,但我确信我已经尽我所能来解决这个问题,但我现在处于停电状态。


// function for the createdImageFile....

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 = getExternalFilesDir(Environment.DIRECTORY_PICTURES);

    File image = File.createTempFile(

            imageFileName,  /* prefix */

            ".jpg",         /* suffix */

            storageDir      /* directory */

    );

我怎样才能把接收到的图像放到这个路径中呢?


皈依舞
浏览 82回答 1
1回答

jeck猫

我假设您想从拍摄的照片中获取 URI。为了从图库和相机中选择图像,您可以尝试这种方式:1)这种从图库中挑选图像的方法private void pickFromGallery(int galleryCode) {&nbsp; &nbsp; URI_REQUEST_CODE = galleryCode;&nbsp; &nbsp; Intent intent;&nbsp; &nbsp; if (Build.VERSION.SDK_INT < 19) {&nbsp; &nbsp; &nbsp; &nbsp; intent = new Intent();&nbsp; &nbsp; &nbsp; &nbsp; intent.setAction(Intent.ACTION_GET_CONTENT);&nbsp; &nbsp; &nbsp; &nbsp; intent.setType("image/*");&nbsp; &nbsp; &nbsp; &nbsp; startActivityForResult(intent, GALLERY_REQUEST_CODE);&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);&nbsp; &nbsp; &nbsp; &nbsp; intent.addCategory(Intent.CATEGORY_OPENABLE);&nbsp; &nbsp; &nbsp; &nbsp; intent.setType("image/*");&nbsp; &nbsp; &nbsp; &nbsp; startActivityForResult(intent, GALLERY_REQUEST_CODE);&nbsp; &nbsp; }}2)这种获取相机拍摄图像的方法(一定要用你的包名替换“com.your.package.name”)//declare a global variable in your scope to provide the URI of image taken with cameraprivate URI uri;private void dispatchTakePictureIntent(int captureCode) {&nbsp; &nbsp; URI_REQUEST_CODE = captureCode;&nbsp; &nbsp; Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);&nbsp; &nbsp; // Ensure that there's a camera activity to handle the intent&nbsp; &nbsp; if (takePictureIntent.resolveActivity(getPackageManager()) != null) {&nbsp; &nbsp; &nbsp; &nbsp; // Create the File where the photo should go&nbsp; &nbsp; &nbsp; &nbsp; File photoFile = null;&nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; photoFile = createImageFile();&nbsp; &nbsp; &nbsp; &nbsp; } catch (IOException ex) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Error occurred while creating the File&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ex.printStackTrace();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; // Continue only if the File was successfully created&nbsp; &nbsp; &nbsp; &nbsp; if (photoFile != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Uri photoURI = FileProvider.getUriForFile(this,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "com.your.package.name",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; photoFile);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; uri = photoURI;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}3)构建onActivityResult方法&nbsp; &nbsp; &nbsp; &nbsp;public void onActivityResult(int requestCode, int resultCode, Intent data) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Result code is RESULT_OK only if the user selects an Image&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; super.onActivityResult(requestCode, resultCode, data);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (resultCode == Activity.RESULT_OK) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (requestCode == GALLERY_REQUEST_CODE) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (URI_REQUEST_CODE == 1) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Uri selectedImage = data.getData();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (selectedImage != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Toast.makeText(this, selectedImage.toString(), Toast.LENGTH_SHORT).show();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Log.i("UriFromGalleryPicture", "onActivityResult: " + selectedImage.toString());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else if (URI_REQUEST_CODE == 2) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (checkPermissionREAD_EXTERNAL_STORAGE(this)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Toast.makeText(this, uri.toString(), Toast.LENGTH_SHORT).show();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Log.i("UriFromCameraPicture", "onActivityResult: " + uri);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }4) 在需要图像 URI 的地方调用 pickFromGallery(1) / dispatchTakePictureIntent(2) 方法让我们知道这是否有帮助,您应该在问题中详细添加您遇到的错误,以及为图像生成的路径
随时随地看视频慕课网APP

相关分类

Java
我要回答