How to delete file programmatically from sd card in android 4.4 and above - android-4.4-kitkat

Actually I am making an app in which I can list all data present in external sd card and can delete a file. But because in android 4.4 and above we cannot write to external sd card. Please help me get out of this..

First of all update your manifest.xml with following code:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE />
Then write:
File file = new File(filePath);
boolean deleted = file.delete();
where filePath is the path of the file you want to delete
for example,
/sdcard/myDirectory/myfile.txt

Related

.NET MAUI app + MediaPicker is crashing with Android.Content.Res.Resources+NotFoundException

In my .NET MAUI app, I use the built-in MediaPicker to access user's photos. The app works fine until I tap the icon to access the photos. It then crashes with the following error message:
Android.Content.Res.Resources+NotFoundException Message=Drawable
com.mycompany.myapp:mipmap/appicon with resource ID #0x7f0d0000
I did replace the app icon recently and I also noticed that the app is not using it when I run the app on an actual device. I simply put a new svg file in the Resources/AppIcon folder with the same name as the original i.e. appicon.svg.
In the properties for this file, I set it as MAUIICON.
This is what's in the project file:
<!-- App Icon -->
<MauiIcon Include="Resources\AppIcon\appicon.svg" />
In case, it's helpful, this is how I'm using the MediaPicker:
try
{
var result = await MediaPicker.PickPhotoAsync(new MediaPickerOptions
{
Title = "Pick Image"
});
if (result == null)
return;
}
catch(Exception e)
{
// Log error
}
I never hit the catch block. It just crashes with the error message I mentioned above.
Any idea how to address this issue?
The issue is definitely the AppIcon. Looks like .NET MAUI is not generating correct size icons from the svg that I had under Resources\AppIcon.
The workaround is to manually create the necessary icons -- in my case, I only created one 72x72 -- and placed them in Platforms\Android\Resources\mipmap folder. In my case, the Resources folder was there under Platforms\Android so I just had to create the mipmap folder.
I then had to go into AndroidManifest.xml file and correctly link them:
<application android:allowBackup="true" android:icon="#mipmap/ic_launcher" android:roundIcon="#mipmap/ic_launcher_round" android:supportsRtl="true"></application>
So, in my case the file name for the app icon I manually generated is ic_launcher.png. I did have to have a second image named ic_launcher_round.png.
Looks like .NET MAUI is supposed to do this work but it's not doing it.
The second bonus -- which it shouldn't be -- is that when I now install the app on a device, I see the correct app icon for my app.
Here's a link to the issue I reported. Anyone experiencing this issue should up vote it so that it'll get the team's attention.
https://developercommunity.visualstudio.com/t/Changing-app-icon-of-NET-MAUI-app-then/10090581

I cant load HTML file into WebView from app specific storage since Android 11 (SDK30)

My App has the following use case:
We download a geojson file, a html file and some audio files from a server for a specific language a user has chosen and we are saving this in the app specific file directory.
The directory path is the following for each use case:
For the HTML file we have:
/storage/emulated/0/Android/data/my.example.com/files/ExampleApp/EnginePayload/de/html-file.html
For the json file we have:
/storage/emulated/0/Android/data/my.example.com/files/ExampleApp/LocationPayload/de/geojson-file.geojson
Audiodata:
/storage/emulated/0/Android/data/my.example.com/files/ExampleApp/Audio/de/intro.mp3
/storage/emulated/0/Android/data/my.example.com/files/ExampleApp/Audio/de/outro.mp3
and so on...
Since i have to change the minSDK and compileSDK to 30 i don't get the files to work.
I load the html-file.getAbsolutePath() to the webView but the webview client does not call ANY callback!
The audio files will not be started, because we use javascript functions to do that.
As i unterstand it, the app specific directory does not have to be changed because even on Android 11, the app has permission to read and write its own created files.
The moment i change it back to SDK 29, everything is working.
I don't know what to do. Can someone help me please?
I fixed it myself after hours!
My assumptions were right. The app had access to the files, because the app created them itself. so there was not an issue here to read the files...
Here is my fix and i hope it will help someone in the future:
There was something missing in the settings for the WebView.
webView.getSettings().setAllowFileAccess(true);
This allows me now to load a html file (with absolute Path) from the app specific directory for Android 11.
Here is my code:
webView.getSettings().setAllowFileAccess(true);
File extDir = cxt.getExternalFilesDir(null);
//htmlPath is something like this: "ExampleApp/EnginePayload/de/html-file.html"
File twine = new File(extDir.toString() + "/" + htmlPath);
webView.loadUrl(twine.getAbsolutePath());

how to change android package name on phonegap build website

i am trying to upload my apk on google play store but i am seeing this error: You need to use a different package name because com.phonegap.www is used by a pre-installed application. To upload a pre-installed application, please contact Google Play Developer Support. Learn more.
You need to use a different package name because "com.phonegap.www" already exists in Google Play. i just export my project as html from anyflip book as maker i didn't follow the phonegap normal build, please tell me how to change my package nameenter image description here
Change it in your config file before you upload anything.
In the root folder you have a file called config.xml. The second line of that file starts with <widget and it has an attribute id="com.phonegap.www". Change it to your own domain name for the app you're building, or to any value that should be globally unique. Then zip your folder, upload it to PhoneGap and try again.

App Inventor sharing files No Activity found to handle Intent { act=android.intent.action.SEND flg=0x1 (has clip) (has extras) }

I'm a beginner at App Inventor and I don't know what I'm doing wrong with the sharing component. I'm trying to share a sound file but it gives me the following error:
No Activity found to handle Intent { act=android.intent.action.SEND flg=0x1 (has clip) (has extras) }
The code is the following:
I also tried the following things:
This doesn't work.
This reproduces the sound perfectly. That means that the picker works and the file exists.
I put a file in my root folder and it works but I want to send the file which was picked from the list which is in the following folder "/storage/sdcard0/MyDocuments/Downloads/app_inventor_1431867799168.mpeg".
I don't know where is the problem and I look in a lot of forums and none of the solutions solved my problem.
Thanks for your help.
PD: I just found that the downloaded files are stored in .MPEG format which it is a compressed format. It is possible that sharing function don't find any app that accept this type of format. If this is the problem please tell me what I have to do to make App Inventor to not change the original .mp3 file.
ok, problem solved as I said in the PD part the problem was that by default App Inventor stores the audio files in to MPEG format. This format is not recognized by whatsapp, gmail, etc...
Solution was to set a name to each file when I download them that ends with .mp3
With this extention the programs of my device recognize them and now I can share my audio files.
Thanks to everyone that was trying to solve this problem.

Reading JPG into BitmapData in ActionScript 3 (JPG is inside APK file)

I'm using Adobe AIR to make APK file for my Android phone. Inside the APK package there is a SWF file and in the same folder as the SWF file there is a subfolder named "images", which contains image.jpg.
I'm trying to read that image.jpg into the program at runtime but can't get the location of it (the program doesn't seem to find it.
Here's what I'm trying:
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
loader.load(new URLRequest("images/image.jpg"));
while (bitmapData==null) {} //wait until loaded
return bitmapData;
The onComplete-function puts sets the bitmapData field. However, the COMPLETE event never happens so the while loop never exists.
I have also tried:
loader.load(new URLRequest("/images/image.jpg"));
loader.load(new URLRequest("app:/images/image.jpg"));
loader.load(new URLRequest("app-storage:/images/image.jpg"));
loader.load(new URLRequest("file:/images/image.jpg"));
None of it works.
I can't get this to work even when trying without the APK file, when only running the SWF file that is generated (the images subfolder is in the same folder as the SWF file).
I can't embed the JPG into the SWF file itself because I need to have a lot of image files and Flash CS6 runs out of memory when making such a big SWF file (reading the images externally would also mean a whole lot less compilation time).
Any ideas? How can I get put the external JPG file into BitmapData? Remember that the image is not outside of the APK file, it is packaged inside it.
Note: I'll also export the thing into a iOS ipa package later so it have to work there as well.
Edit because I can't self-answer due to my reputation.
Strille made me realize that the ActionScript Virtual Machine is single-threaded.
So it's impossible to make a thread sleep (wait) for an image being loaded. Instead I have to sigh rewrite a lot of code so that things are halted and continues when the onComplete function is called.
This thing that I tried before actually works but couldn't complete due to the while loop:
loader.load(new URLRequest("app:/images/image.jpg"));
If I remember correctly, you should be able to do it like this:
loader.load(new URLRequest(new File("app:/images/image.jpg").url));
Since we're using the File class, the above will not run in the Flash Player, just on iOS or Android.
From my experience there are two practical methods to load assets from a running Android APP using AIR
using URLLoader and .url property of the File instance
using FileStream (which receives a File instance)
Adding to project
You can package the files inside the APK using
the same technique as packaging ICONS, inside the .xml descriptor file for your app.
And make sure you add those to your IDE in the Android section (applies to IntelliJ and Flash Builder) "Files and folders to package"
- add the path to your wanted assets folder, and give it a relative name alias.
Elaboration
The assets themselves are placed (when the APP is installed),
inside the ''applicationDirectory'' which is available for AIR,
Yet since newer Android OS's, and Flash player security issues,
it is not possible to access those assets via the nativePath,
only the 'url' property of the File instance.
Best Practice
Use URLLoader and .url property of the File instance,
as it is also Asynchronous by nature and can resolve PNG / JPG files
using native decoder instead of having to do this manually
reading a FileStream (which returns a a ByteArray)
var f:File = File.applicationDirectory.resolvePath('images/myimage.png');
loader.load(new URLRequest(f.url));
and,
If I'm missing more ways, please let know... )

Resources