0

Login pageI am coding the add fragment. That is on the right of the bottom navigation menu. But when i press add on the navigation it keeps navigating to the home page. And when it works properly i can not add a photo picker to users to pick image from gallery. My code is belowenter image description here

package com.example.uninotes;

import static android.app.Activity.RESULT_OK;
import static android.content.ContentValues.TAG;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;

import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.core.app.ActivityCompat;
import androidx.fragment.app.Fragment;

public class AddFragment extends Fragment {

    ImageView imgGallery;
    Button btnGallery;


    ActivityResultLauncher<Intent> activitylauncher = registerForActivityResult(
            new ActivityResultContracts.StartActivityForResult(),
            new ActivityResultCallback<ActivityResult>() {
                @Override
                public void onActivityResult(ActivityResult result) {
                    Log.d(TAG, "onActivityResult: ");
                }
            }
    );

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_add,container,false);
        btnGallery = getView().findViewById(R.id.clickable);
        imgGallery = getView().findViewById(R.id.imageView2);

        btnGallery.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(Intent.ACTION_PICK,MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                activitylauncher.launch(intent);
            }
        });

        return view;
    }
}

I was trying to make the user pick image from the gallery.

  • Did you add the correct permission into the manifest? https://developer.android.com/guide/topics/permissions/overview – GabeRAMturn Apr 28 '23 at 12:08
  • @GabeRAMturn, please tell which permissions. This is too vague. There are no permissions needed to my knowledge. – blackapps Apr 28 '23 at 12:10
  • Well, considering you‘re trying to access files, you‘d probably need to add file system permissions into the manifest. – GabeRAMturn Apr 28 '23 at 12:12
  • I am checking it right now –  Apr 28 '23 at 12:13
  • I added the permissions internet into the manifest file –  Apr 28 '23 at 12:16
  • https://developer.android.com/training/data-storage/shared/photopicker https://android-developers.googleblog.com/2023/04/photo-picker-everywhere.html – Abdullah Javed Apr 28 '23 at 13:13
  • @GabeRAMturn, No, certainly not if you let the user pick files. No permissions needed. Please dont confuse poster. – blackapps Apr 28 '23 at 13:36
  • Why isn't my activitylauncher working?? i ve been stuck to it for 2 days. Please help, where am i mistaking –  Apr 28 '23 at 13:42
  • I think you would need permissions to read the contents of the device for selecting image from the device. Refer to the below link. https://stackoverflow.com/a/32617585/5362509 – Lakshmi Apr 29 '23 at 07:13
  • I cannot use permission inside the fragment either. Program says 'cannot resolve symbol READ_EXTERNAL_STORAGE' , even though it is in my manifests file. –  May 06 '23 at 11:53

0 Answers0