第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何將圖像文件從相機添加到我創(chuàng)建的路徑文件

如何將圖像文件從相機添加到我創(chuàng)建的路徑文件

皈依舞 2023-06-08 13:55:46
好的,我一直在尋找解決此問題的方法,我從相機或圖庫中捕獲圖像并創(chuàng)建了圖像文件路徑但不幸的是我可以獲得圖像,因為來自相機或圖庫的圖像不在創(chuàng)建的文件路徑中實際上。那么我該如何解決呢?我真的不知道在這里說什么,但我確信我已經(jīng)盡我所能來解決這個問題,但我現(xiàn)在處于停電狀態(tài)。// 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 */    );我怎樣才能把接收到的圖像放到這個路徑中呢?
查看完整描述

1 回答

?
jeck貓

TA貢獻1909條經(jīng)驗 獲得超7個贊

我假設您想從拍攝的照片中獲取 URI。為了從圖庫和相機中選擇圖像,您可以嘗試這種方式:


1)這種從圖庫中挑選圖像的方法


private void pickFromGallery(int galleryCode) {

    URI_REQUEST_CODE = galleryCode;

    Intent intent;

    if (Build.VERSION.SDK_INT < 19) {

        intent = new Intent();

        intent.setAction(Intent.ACTION_GET_CONTENT);

        intent.setType("image/*");

        startActivityForResult(intent, GALLERY_REQUEST_CODE);

    } else {

        intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);

        intent.addCategory(Intent.CATEGORY_OPENABLE);

        intent.setType("image/*");

        startActivityForResult(intent, GALLERY_REQUEST_CODE);

    }

}

2)這種獲取相機拍攝圖像的方法(一定要用你的包名替換“com.your.package.name”)


//declare a global variable in your scope to provide the URI of image taken with camera

private URI uri;


private void dispatchTakePictureIntent(int captureCode) {

    URI_REQUEST_CODE = captureCode;

    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

    // Ensure that there's a camera activity to handle the intent

    if (takePictureIntent.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

            ex.printStackTrace();

        }

        // Continue only if the File was successfully created

        if (photoFile != null) {

            Uri photoURI = FileProvider.getUriForFile(this,

                    "com.your.package.name",

                    photoFile);

            uri = photoURI;

            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);

            startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);

        }

    }

}

3)構(gòu)建onActivityResult方法


       public void onActivityResult(int requestCode, int resultCode, Intent data) {

            // Result code is RESULT_OK only if the user selects an Image

            super.onActivityResult(requestCode, resultCode, data);

            if (resultCode == Activity.RESULT_OK) {

                if (requestCode == GALLERY_REQUEST_CODE) {

                    if (URI_REQUEST_CODE == 1) {

                        Uri selectedImage = data.getData();

                        if (selectedImage != null) {

                            Toast.makeText(this, selectedImage.toString(), Toast.LENGTH_SHORT).show();

                            Log.i("UriFromGalleryPicture", "onActivityResult: " + selectedImage.toString());

                        }

                    } else if (URI_REQUEST_CODE == 2) {

                        if (checkPermissionREAD_EXTERNAL_STORAGE(this)) {

                            Toast.makeText(this, uri.toString(), Toast.LENGTH_SHORT).show();

                            Log.i("UriFromCameraPicture", "onActivityResult: " + uri);

                    }

                }

4) 在需要圖像 URI 的地方調(diào)用 pickFromGallery(1) / dispatchTakePictureIntent(2) 方法


讓我們知道這是否有幫助,您應該在問題中詳細添加您遇到的錯誤,以及為圖像生成的路徑


查看完整回答
反對 回復 2023-06-08
  • 1 回答
  • 0 關注
  • 121 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號