Recent

Author Topic: [SOLVED] WRITE_EXTERNEL_STORAGE Failed in ANDROID 14  (Read 6182 times)

Guser979

  • Jr. Member
  • **
  • Posts: 54
[SOLVED] WRITE_EXTERNEL_STORAGE Failed in ANDROID 14
« on: April 09, 2024, 01:46:05 am »
I can't get "WRITE EXTERNEL STORAGE" permission

my MANIFEST

  <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" tools:remove="android:maxSdkVersion" tools:ignore="ScopedStorage"/>


Inside the APP I am using:

        manifestPermissions[1]:= 'android.permission.WRITE_EXTERNAL_STORAGE" tools:remove="android:maxSdkVersion" tools:ignore="ScopedStorage';


I keep getting permission denied error

ANDROID 14
« Last Edit: April 13, 2024, 08:42:36 pm by Guser979 »

maxerist

  • New Member
  • *
  • Posts: 28
Re: WRITE_EXTERNEL_STORAGE Failed
« Reply #1 on: April 09, 2024, 10:26:03 am »
I can't get "WRITE EXTERNEL STORAGE" permission
.....

I keep getting permission denied error

ANDROID 14


I'm not sure this is LAMW-specific issue. You didn't provide details about the location you're trying to write, but you may trying reading answers to  googling result [ WRITE_EXTERNAL_STORAGE "android 12..14" site:stackoverflow.com "permission denied" ] shows many useful answers where mostly Android 11+ restricts usage of this setting. The quote from the docs  usually mentioned  in the answers is

Quote
If your app targets Android 11, both the WRITE_EXTERNAL_STORAGE permission and the WRITE_MEDIA_STORAGE privileged permission no longer provide any additional access.

Guser979

  • Jr. Member
  • **
  • Posts: 54
Re: WRITE_EXTERNEL_STORAGE Failed
« Reply #2 on: April 09, 2024, 11:07:28 am »
Thank you for your attempted response. But the way I'm trying is correct, right?
 
  I'll wait a little longer for anyone who has tried writing a file to the Downloads folder or another folder in storage.

I believe many people have already done this in LAMW. (or not ? !!!! )  .

Location where I try to write: Downloads folder

[UPDATE]

I searched for LAMW demos that used Self.GetEnvironmentDirectoryPath(dirDownloads) and found a list. Most with errors.
  But a demo managed to do what I need. Now I tried to adapt this code in my app. If it works, I'll flag it as [Solved]

ColorPicker
AppXLSWriterDemo1
AppUploadServiceDemo1
AppShareFileDemo1
AppSFTPClientDemo1
AppQRGenDemo1
AppPublicFoldersAccessDemo1   <---------------------------------it worked
AppOpenFileDialogDemo1
AppMediaPlayerDemo1
AppLoadImageVideoSoundFromInternet
appLamwUnzipDemo
AppJCenterEmailReceiverDemo1
AppIntentDemoPrinting1
AppIntentDemo2
AppHttpClientDemo1
AppFTPClientDemo1
AppDrawingViewDemo2
AppDrawingViewDemo1
AppDownloadServiceDemo1
AppDemo1
AppCameraDemo2
AppBluetoothServerSocketDemo2
AppBluetoothDemo1
AppBluetoothClientSocketDemo2
AppBarcodeGenDemo1


UPDATE 2

Unfortunately it doesn't work for what I need.

This Demo uses Self.RequestOpenFile . Opens a dialog to create or open the file.

That's not what I need.
I need to create the file without "dialog"
« Last Edit: April 09, 2024, 02:06:24 pm by Guser979 »

Guser979

  • Jr. Member
  • **
  • Posts: 54
Re: WRITE_EXTERNEL_STORAGE Failed
« Reply #3 on: April 10, 2024, 04:02:25 pm »
Unsuccessfully .

Researching I found that it would be necessary to use intent.

I made it :
setAction(Settings.ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION)

So when I start my app it presents me with the dialog to allow access to all files. I give permission, but it still doesn't work.

In LAMW I noticed that GetEnvironmentDirectoryPath(dirDCIM) returns the path of my application and not exactly the DCIM folder.

In short, it is practically impossible to do anything that requires accessing, importing or exporting files. Be it a document, text, photo, database (sqlite), etc.

I would like to ask anyone who can do this:

Update the Appcamerademo2 DEMO to make it work.

This would help us as this demo tries to write the photo but fails.

Once this was fixed, it would be easier to continue.

jmpessoa

  • Hero Member
  • *****
  • Posts: 2312
Re: WRITE_EXTERNEL_STORAGE Failed
« Reply #4 on: April 10, 2024, 07:28:09 pm »
Quote
AppPublicFoldersAccessDemo1   <---------------------------------it worked

Yes, This is a requirement Android > 10 ... It's the best we got for Android > 10 ....

(and with each new version more restrictions are placed on access to "public" folders........)
Lamw: Lazarus Android Module Wizard
https://github.com/jmpessoa/lazandroidmodulewizard

Guser979

  • Jr. Member
  • **
  • Posts: 54
Re: WRITE_EXTERNEL_STORAGE Failed
« Reply #5 on: April 10, 2024, 10:12:06 pm »
Quote
AppPublicFoldersAccessDemo1   <---------------------------------it worked

Yes, This is a requirement Android > 10 ... It's the best we got for Android > 10 ....

(and with each new version more restrictions are placed on access to "public" folders........)


This is really bad.
I have this app (made entirely in LAWM) that fails whenever I have to perform operations where I need to use folders outside of it.

In short, the app maintains a database with locations and objects found in them.

In order not to lose this work, would it be possible to resurrect it with Android STUDIO (I would have to install it)? Or would it be wasted time?

Guser979

  • Jr. Member
  • **
  • Posts: 54
Re: WRITE_EXTERNEL_STORAGE Failed
« Reply #6 on: April 10, 2024, 11:42:40 pm »
I'm trying with ACTION_CREATE_DOCUMENT

The question here is:

Is it possible to send the initial DIR without showing the dialog? How to do this ?
That would solve the problem.

Sorry if I'm not understanding this.
If every time I need to save a file a dialog appears that affects the operation.

public void RequestCreateFile(String _uriAsString, String _fileMimeType, String _fileName, int _requestCode) {
       
       // Requires API level 19
       if( android.os.Build.VERSION.SDK_INT < 19 ) return;

        Uri pickerInitialUri = Uri.parse(_uriAsString);
       
        // Requires API level 19
        Intent intent = new Intent(Intent.ACTION_CREATE_DOCUMENT);
        intent.addCategory(Intent.CATEGORY_OPENABLE);
        intent.setType(_fileMimeType); //"application/pdf"

        //For use with ACTION_CREATE_DOCUMENT to specify an initial file name.
        intent.putExtra(Intent.EXTRA_TITLE, _fileName); //"invoice.pdf"

        // Optionally, specify a URI for the directory that should be opened in
        // the system file picker when your app creates the document.
        if (Build.VERSION.SDK_INT >= 26) {
            //[ifdef_api26up]
            intent.putExtra(DocumentsContract.EXTRA_INITIAL_URI, pickerInitialUri);
            //[endif_api26up]
        }
        controls.activity.startActivityForResult(intent, _requestCode);
    }

Guser979

  • Jr. Member
  • **
  • Posts: 54
Re: WRITE_EXTERNEL_STORAGE Failed
« Reply #7 on: April 11, 2024, 03:20:15 am »
Still trying to solve the problem, I made some adjustments and the application is functional. Makes all recordings (database and photos) within it. (Internal storage)

I compressed the database and photos into a zip file.

Now I just need to export this file.

Any ideas on how to do this?

I thought about sending it by email or to Google Drive.

Any suggestions on how to do it?

UPDATE :

Jintent iasendto fail "Sorry, Not find an application that fulfills the requirement...'"
« Last Edit: April 11, 2024, 04:56:21 am by Guser979 »

Leledumbo

  • Hero Member
  • *****
  • Posts: 8766
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: WRITE_EXTERNEL_STORAGE Failed
« Reply #8 on: April 11, 2024, 01:43:35 pm »
You can not achieve what you want anymore, not with recent Android version. The restriction comes from Android itself, only apps like a file manager and antivirus can have such a permission, the rest can only have user granted directory selection, it can not be dialogless. Not sure about the possibility to circumvent it for sideloaded apps, but here's a reading.

Guser979

  • Jr. Member
  • **
  • Posts: 54
Re: WRITE_EXTERNEL_STORAGE Failed
« Reply #9 on: April 11, 2024, 05:26:48 pm »
Yes. I've given up on that idea for now.

Now I just need to send the file by email. Unfortunately with jintent and iaSendto it doesn't work.

I managed to make gmail open with Jintent and sendto by adding this to Manifest

<queries>
     <intent>
         <action android:name="android.intent.action.SEND"/>
         <data android:mimeType="*/*" />
     </intent>
</queries>

  but Jintent ignores the attachment I send.

I note that despite these restrictions, many apps are able to export data.

A password manager, for example. Export passwords to csv. And several other apps do this. Even the ones that aren't on the PlayStore.

So what we lack is knowledge. I'll keep trying. If I succeed I will post the solution.

a more serious app ends up extremely limited if it is not possible to export or share

LAMW is too powerful to waste. I believe it is worth trying to learn more about the subject.
« Last Edit: April 11, 2024, 05:38:24 pm by Guser979 »

Guser979

  • Jr. Member
  • **
  • Posts: 54
Re: WRITE_EXTERNEL_STORAGE Failed
« Reply #10 on: April 11, 2024, 10:05:15 pm »
It is possible to write to a folder outside the app, (without uri, without intent ?) , without dialog in Android 14:

I compiled an app at MIT (App Inventor). It only has one button on the screen. When you click, a file will be written to the Documents folder. "test.txt".

Anyway, yes it is possible. We just need to know how to do it.

I'll try to decompile it to understand.

Anyone who wants to compile on MIT (APP Inventor) ....
« Last Edit: April 11, 2024, 10:11:48 pm by Guser979 »

Guser979

  • Jr. Member
  • **
  • Posts: 54
Re: WRITE_EXTERNEL_STORAGE Failed
« Reply #11 on: April 12, 2024, 09:55:17 pm »
I got a temporary solution to export all data.

In the following example I compress all the photos taken.
Then use RequestCreateFile from LAWM.

In "ActivityResult" I finish saving the zip file in Downloads using "SaveBytesToUri"

Problem: I found it slow to finish writing. caused by Loop  FOR....  to pass the data from Tfilestream to the array.

But for now it's better than not being able to export the data.


Code: Pascal  [Select][+][-]
  1.  //////  call zip ( from Button , Menu , etc.)
  2.  
  3.  
  4.   procedure TAndroidModule3.zip;
  5.    var
  6.   zip: TZipper;
  7.   L: TStrings;
  8.   i: Integer;
  9.   relativefn: String;
  10.    begin
  11. zip := TZipper.Create;
  12.   try
  13.     zip.Filename := 'exportado.zip';
  14.     L := TStringList.Create;
  15.     try
  16.       FindAllFiles(L,GetEnvironmentDirectoryPath(dirDCIM), '*.jpg', true);
  17.    
  18.       WriteLn(L.Count);
  19.       for i:=0 to L.Count-1 do begin
  20.         relativefn := CreateRelativePath(L[i],GetEnvironmentDirectoryPath(dirDownloads));
  21.         zip.Entries.AddFileEntry(SysToUTF8(L[i]), relativefn);
  22.       end;
  23.     finally
  24.       L.Free;
  25.     end;
  26.    
  27.     zip.SaveToFile(GetEnvironmentDirectoryPath(dirDownloads)+'/exportado.zip');
  28.    Self.RequestCreateFile(Self.GetEnvironmentDirectoryPath(dirDownloads),'application/zip','exportado.zip', 111);   //handled by "OnActivityResult"
  29.  
  30.  
  31.  
  32.   finally
  33.     zip.Free;
  34.   end;
  35.  end;
  36.  
  37.  
  38.  
  39.  
  40.  
  41.  
  42. procedure TAndroidModule3.AndroidModule3ActivityResult(Sender: TObject;
  43.   requestCode: integer; resultCode: TAndroidResult; intentData: jObject);
  44. var
  45.   treeUri,uri: jObject;
  46.   arrayData: TDynArrayOfString;
  47.   listData: TstringList;
  48.   cnt,count, i: integer;
  49.   contentText: string;
  50.   arrayb: Tdynarrayofjbyte;
  51.   b:byte;
  52.   FS: TFileStream;
  53.   outFileName, outUriValue: string;
  54.  
  55. begin
  56.    if resultCode = RESULT_OK then
  57.    begin
  58.  
  59.       if intentData = nil then
  60.       begin
  61.          ShowMessage('Sorry... data nil received...');
  62.          Exit;
  63.       end;
  64.  
  65.       treeUri:= IntentManager1.GetDataUri(intentData);
  66.        Uri:= IntentManager1.GetDataUri(intentData);
  67.      
  68.  
  69.       if treeUri = nil then
  70.       begin
  71.          ShowMessage('Sorry... Uri nil received...');
  72.          Exit;
  73.       end;
  74.  
  75.  if requestCode = 111 then  //create file
  76.          begin
  77.        
  78.       FS := TFileStream.Create(GetEnvironmentDirectoryPath(dirDownloads)+'/exportado.zip', fmOpenRead or fmShareDenyWrite);
  79.           SetLength( arrayb, fs.Size);   fs.Position:=0;
  80.        
  81.          
  82.   Fs.Read(arrayb[0], fs.Size);
  83.  
  84.  self.SaveBytesToUri(arrayb,treeUri);
  85.  
  86. // release the filestream
  87.  Fs.free;
  88.  
  89. end;                        
  90.  
  91.  



UPDATE : Slowness problem resolved. Just eliminate the FOR.... Unnecessary because Fs.Read(arrayb[0], fs.Size); do the reading.

UPDATE : In activityresult: release the filestream


The code has been edited.   
« Last Edit: July 16, 2024, 03:39:26 am by Guser979 »

magleft

  • Full Member
  • ***
  • Posts: 117
Re: [SOLVED] WRITE_EXTERNEL_STORAGE Failed in ANDROID 14
« Reply #12 on: May 27, 2024, 03:35:13 pm »
Hello All.
Guser979  I need your help. I am facing the same problem as you.
My app needs to copy the database it uses to a shared folder.
Using your example, I created the compressed file in the downloads folder, but the entire path to the file is saved. This is something I would not want to happen. I am attaching your code as I modified it:

Code: Pascal  [Select][+][-]
  1. procedure TFmaster.CreateZipFile;
  2. var
  3.    zip: TZipper;
  4.    i: Integer;
  5.    relativefn: String;
  6. begin
  7.      zip := TZipper.Create;
  8.      try
  9.         zip.Filename :=zipFile ;
  10.         try
  11.                  relativefn:=self.GetEnvironmentDirectoryPath(dirDatabase)+'/'+dbname  ;
  12.                zip.Entries.AddFileEntry(PChar(relativefn));
  13.         finally
  14.                L.Free;
  15.         end;
  16.         zip.SaveToFile(GetEnvironmentDirectoryPath(dirDownloads)+'/'+zipFile);
  17.         Self.RequestCreateFile(Self.GetEnvironmentDirectoryPath(dirDownloads),'application/zip',zipFile, 111);   //handled by "OnActivityResult"
  18.      finally
  19.         zip.Free;
  20.      end;
  21. end;
  22.  
  23.  

What should I fix?
Also, how could I decompress the file inside the "dirDatabase" folder of the application?

Thanks in advance for your time and help.
windows 10 64

Guser979

  • Jr. Member
  • **
  • Posts: 54
Re: [SOLVED] WRITE_EXTERNEL_STORAGE Failed in ANDROID 14
« Reply #13 on: June 13, 2024, 10:24:51 pm »
The error is probably here:
Your code:
Code: Pascal  [Select][+][-]
  1.    relativefn:=self.GetEnvironmentDirectoryPath(dirDatabase)+'/'+dbname ;
  2.      
Try readapting your code to relative path:
Code: Pascal  [Select][+][-]
  1.  relativefn := CreateRelativePath(L[i],GetEnvironmentDirectoryPath(dirDownloads));  


L is the file path



I haven't tested unzip but you can study the zipper unit and try. There are examples in the source folder


LAMW4Windows\fpc\3.2.0\source\fixes_3_2\packages\paszlib\examples\fpunzipper.lpi
« Last Edit: June 13, 2024, 11:04:31 pm by Guser979 »

magleft

  • Full Member
  • ***
  • Posts: 117
Re: [SOLVED] WRITE_EXTERNEL_STORAGE Failed in ANDROID 14
« Reply #14 on: June 15, 2024, 03:05:01 pm »
Thanks. I will try it.
windows 10 64

 

TinyPortal © 2005-2018