简体   繁体   English

在我的应用程序中,我无法将我从相机拍摄的照片传输到应用程序

[英]In my app I can't transfer the picture I took from the Camera to the app

I made an automobile management application.我做了一个汽车管理应用程序。

I made a vehicle image adding system in the application.我在应用程序中制作了一个车辆图像添加系统。

When the Image is selected I want to upload it to firebase and view it.选择图像后,我想将其上传到 firebase 并查看。

When I select a picture from the gallery, I can upload and view it, it works without any problems.当我 select 图片库中的图片时,我可以上传并查看它,它可以正常工作。

At the same time, when I select Camera, I ask for permission and access the camera, but this is the part that doesn't work.同时,当我 select 相机时,我请求许可并访问相机,但这是不起作用的部分。 After I take the picture from the camera, I return to the application, but the picture I took is not visible.从相机拍摄照片后,我返回到应用程序,但我拍摄的照片不可见。 I don't know how to do this.我不知道该怎么做。

And.. I don't know how to code.而且..我不知道如何编码。

I'm trying to watch video lessons and adapt them the way I want to make the applications in my head.我正在尝试观看视频课程并按照我想要在脑海中制作应用程序的方式对其进行调整。

This application is the last point I came to.这个应用程序是我来到的最后一点。

As far as I saw the answers to the previous questions and the videos, I could not answer this question.就我看到前面问题的答案和视频,我无法回答这个问题。

I know it will be difficult, but can you show this ignorant the correct method?我知道这会很困难,但是你能告诉这个无知的人正确的方法吗?

` `

public class UploadActivity extends AppCompatActivity {

    Uri imageData;
    ActivityResultLauncher<Intent> activityResultLauncher;
    ActivityResultLauncher<String> permissionLauncher;
    private ActivityUploadBinding binding;
    private FirebaseStorage firebaseStorage;
    private FirebaseFirestore firebaseFirestore;
    private FirebaseAuth firebaseAuth;
    private StorageReference storageReference;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //Burada Yazan kod ustteki standart titleyi kaldirmak icin.
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
        getSupportActionBar().hide();
        // Burada Bitiyor.

        binding = ActivityUploadBinding.inflate(getLayoutInflater());
        View view = binding.getRoot();
        setContentView(view);

        registerLauncher();


        //Firebase fireStorage icin
        firebaseStorage = FirebaseStorage.getInstance();
        firebaseFirestore = FirebaseFirestore.getInstance();
        firebaseAuth = FirebaseAuth.getInstance();
        storageReference = firebaseStorage.getReference();

` `

` `

public void selectImage(View view) {

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("Resim Seciniz");
    builder.setMessage("Resim Kaynagini Seciniz");

    builder.setPositiveButton("Camera", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {

            /* if(ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA)
            != PackageManager.PERMISSION_GRANTED
            || ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
            != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(this, new String[]{
                Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE); */

            if (ContextCompat.checkSelfPermission(UploadActivity.this,Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
                if(ActivityCompat.shouldShowRequestPermissionRationale(UploadActivity.this,Manifest.permission.CAMERA)) {
                    Snackbar.make(view,"Kameraniza Erismek icin izin gereklidir.",Snackbar.LENGTH_INDEFINITE).setAction("izin Ver", new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            permissionLauncher.launch(Manifest.permission.CAMERA);
                        }
                    }).show();
                } else {
                    permissionLauncher.launch(Manifest.permission.CAMERA);
                }
            } else {
                Intent takePicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                activityResultLauncher.launch(takePicture);
            }


            dialogInterface.dismiss();
        }
    });

    builder.setNeutralButton("Cancel", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
            dialogInterface.dismiss();
        }
    });

    builder.setNegativeButton("Gallery", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {

            if(ContextCompat.checkSelfPermission(UploadActivity.this,Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
                if(ActivityCompat.shouldShowRequestPermissionRationale(UploadActivity.this,Manifest.permission.READ_EXTERNAL_STORAGE)) {
                    Snackbar.make(view,"Galerinize erişmek için izin gereklidir.",Snackbar.LENGTH_INDEFINITE).setAction("İzin Ver", new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            //izin isteyecegiz.
                            permissionLauncher.launch(Manifest.permission.READ_EXTERNAL_STORAGE);

                        }
                    }).show();
                } else {
                    //Ekstra izin isteyecegiz.
                    permissionLauncher.launch(Manifest.permission.READ_EXTERNAL_STORAGE);
                }
            } else {
                // Zaten izin verilmis ise.
                Intent pickPhoto = new Intent(Intent.ACTION_PICK,
                        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                activityResultLauncher.launch(pickPhoto);
            }


            dialogInterface.dismiss();
        }
    });

    AlertDialog dialog = builder.create();
    dialog.show();

}

private void registerLauncher() {

    activityResultLauncher = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), new ActivityResultCallback<ActivityResult>() {
        @Override
        public void onActivityResult(ActivityResult result) {
            if (result.getResultCode() == RESULT_OK) {
                Intent intentFromResult = result.getData();
                if(intentFromResult != null) {
                    imageData = intentFromResult.getData();
                    binding.addCarPicture.setImageURI(imageData);
                }
            }

        }
    });

    permissionLauncher = registerForActivityResult(new ActivityResultContracts.RequestPermission(), new ActivityResultCallback<Boolean>() {
        @Override
        public void onActivityResult(Boolean result) {
            if(result) {
                Intent intentToGallery = new Intent(Intent.ACTION_PICK,MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                activityResultLauncher.launch(intentToGallery);

            } else {
                Toast.makeText(UploadActivity.this,"iznine ihtiyacimiz var",Toast.LENGTH_LONG).show();
            }
        }
    });
}

` `

A couple of things.几件事。 You should use request codes, so you can check for it in you activity result method and use some logic to distinguish between taking a picture and selecting a photo from the gallery.你应该使用请求代码,这样你就可以在你的活动结果方法中检查它,并使用一些逻辑来区分拍照和从图库中选择照片。 Because the Gallery and Camera return different different things.因为 Gallery 和 Camera 返回的东西不同。 Second, the image taken from the camera isn't saved.其次,不保存从相机拍摄的图像。 It doesn't have a URI location, because it's just in memory.它没有 URI 位置,因为它就在 memory 中。

if(intentFromResult != null) {
       imageData = intentFromResult.getData();
       Bitmap img = (Bitmap)(data.getExtras().get()'data'));
       //Upload img to Database
       //Set imageView from Database
}

Here is the documentation on the way you are trying to implement it:这是有关您尝试实施它的方式的文档:
https://developer.android.com/training/camera-deprecated/photobasics https://developer.android.com/training/camera-deprecated/photobasics

Here is the current documentation on how it should be implemented: https://developer.android.com/guide/topics/media/camera这是关于如何实施的当前文档: https://developer.android.com/guide/topics/media/camera

And one last bit to think about.最后一点要考虑。 It might be better to use a library so you don't have to write a whole new implementation.使用库可能更好,这样您就不必编写全新的实现。 I've used https://github.com/Dhaval2404/ImagePicker before, it's ok.之前用过https://github.com/Dhaval2404/ImagePicker ,没问题。 There might be a better one out there that suits your needs.可能有更好的适合您的需求。 Take your time and look around.慢慢来,环顾四周。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在我的 React 本机应用程序中覆盖 AmplifyTheme - How can I override the AmplifyTheme in my react native app 如何将我的应用程序从 firebase 获取的数据设置到我的 bottomSheetView? (科特林) - How can I set the fetched data of my app from firebase to my bottomSheetView? (Kotlin) 我可以同时使用 jcenter() 和 mavenCentral() 在我的应用程序中实现 Firebase 吗? - Can I use both jcenter() and mavenCentral() to implement Firebase in my app? 如何确保用户在没有任何用户身份验证的情况下只能在我的 ReactJS 应用程序上投票一次? - How can I Ensure a User can only Vote Once on my ReactJS App Without any User Authentication? 如何在我的 Cloud-Run Flask 应用程序中获取用户的 IP 地址? - How can I get the user's IP-Address in my Cloud-Run Flask app? Firebase:如何在应用检查中注册我的 Flutter 应用? - Firebase: How do I register my Flutter app in app check? 将注册用户从一个 firebase 应用程序转移到另一个 - Transfer registered users from one firebase app to another 如何将我的 react 原生网站链接到我的应用程序? - How do I link my react native website to my app? 我在我的 todo 应用程序中遇到 null 安全问题 - I'm having issue with null safety in my todo app 如何将我的 MAUI 应用程序连接到生产环境中的 Firestore 数据库? - How do I connect my MAUI app to a Firestore database in production?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM