මේ ANDROID file chooser එකේ full path එක ගන්නේ කොහොමද

elajani

Well-known member
  • Oct 18, 2020
    10,573
    6,344
    113
    launchSomeActivity = registerForActivityResult(
    new ActivityResultContracts.StartActivityForResult(),
    new ActivityResultCallback<ActivityResult>() {
    @Override
    public void onActivityResult(ActivityResult result) {
    if (result.getResultCode() == Activity.RESULT_OK) {
    Intent data = result.getData();
    File sourceLocation= new File (data.getData().getPath());
    txtbxname.setText(sourceLocation.getName());
    File targetLocation= new File ("/storage/sdcard0/Pictures/Screenshots/");

    InputStream in = null;
    OutputStream out=null;
    try {
    in = new FileInputStream(sourceLocation);
    out= new FileOutputStream(targetLocation);
    } catch (IOException e) {
    e.printStackTrace();
    }
    // Copy the bits from instream to outstream
    byte[] buf = new byte[1024];
    int len;
    try {


    while ((len = in.read(buf)) > 0) {
    out.write(buf, 0, len);
    }
    in.close();
    out.close();
    } catch (IOException e){

    e.printStackTrace();
    }


    }
    }
    });
     
    • Like
    Reactions: Solo Rider

    IreshMM

    Well-known member
  • Jan 20, 2014
    1,546
    444
    83
    I'm not a android guy to answer the question but generally I would look in the class documentation for the specific method. By the way, you could have used something like below to display the code more clearer.

    [CODE=java]Your code here[/CODE]

    Java:
    launchSomeActivity = registerForActivityResult(
            new ActivityResultContracts.StartActivityForResult(),
            new ActivityResultCallback<ActivityResult>() {
                @Override
                public void onActivityResult(ActivityResult result) {
                    if (result.getResultCode() == Activity.RESULT_OK) {
                        Intent data = result.getData();
                        File sourceLocation= new File (data.getData().getPath());
                        txtbxname.setText(sourceLocation.getName());
                        File targetLocation= new File ("/storage/sdcard0/Pictures/Screenshots/");
    
                        InputStream in = null;
                        OutputStream out=null;
                        try {
                            in = new FileInputStream(sourceLocation);
                            out= new FileOutputStream(targetLocation);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                        // Copy the bits from instream to outstream
                        byte[] buf = new byte[1024];
                        int len;
                        try {
    
    
                            while ((len = in.read(buf)) > 0) {
                                out.write(buf, 0, len);
                            }
                            in.close();
                            out.close();
                        } catch (IOException e){
    
                            e.printStackTrace();
                        }
    
    
                    }
                }
            });
     
    Last edited:

    Solo Rider

    Well-known member
  • Sep 4, 2020
    35,924
    1
    157,340
    113
    98
    හෝමාගම
    01. Mokadda oya karanna hadanne, Mokatada full path eka?

    02. Image ekak ScreenShots directory ekata save karanna nan hadanne,

    Android 11 wala, dan mehema path append karala file save karanna baha.
    restricted.
    MediaStore API eka use karanna. Google 'saving images via Media Store Api'

    03. Nikan parana phone eheka nan test karanne ( Android 10 & below ),
    'requestLegacyExternalStorage=true' meka manifest ekata danna.
     

    elajani

    Well-known member
  • Oct 18, 2020
    10,573
    6,344
    113
    01. Mokadda oya karanna hadanne, Mokatada full path eka?

    02. Image ekak ScreenShots directory ekata save karanna nan hadanne,

    Android 11 wala, dan mehema path append karala file save karanna baha.
    restricted.
    MediaStore API eka use karanna. Google 'saving images via Media Store Api'

    03. Nikan parana phone eheka nan test karanne ( Android 10 & below ),
    'requestLegacyExternalStorage=true' meka manifest ekata danna.
    Image එකක් move කරන්න හදන්නේ
    Folder එකකින් thava Folder එකකට
     

    elajani

    Well-known member
  • Oct 18, 2020
    10,573
    6,344
    113
    කරගත්තද වැඩේ.
    1 - ok <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    2- ok Android 8 - android:requestLegacyExternalStorage="true"
    Java:
    btnImgCopy.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
    
            if (ContextCompat.checkSelfPermission(MainActivity.this,Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
                File file = new File("/storage/emulated/0/Download/1.jpg");
                Uri uri = Uri.fromFile(file);
                Bitmap bmpimg = null;
            try {
                bmpimg = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
            } catch (Exception e) {
                e.printStackTrace();
            }
            imgviwe.setImageBitmap(bmpimg);
                }
            else{
                System.out.println("*************************************************");
            }
            }
    });

    Image eka ImgViwe eke load venne na....