Can't create folder and file - android

I'm trying to create folder in internal storage of the device, and have some problems with that. I can't create directory using :
String rootPath=Environment.getExternalStorageDirectory().getPath()+"/test";
File file=new File(rootPath);
if(!file.exists()){
file.mkdir();
}
file.mkdir return false
I have such permissions:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_INTERNAL_STORAGE" />
Thank you very much for answers !

So really, as you all guys said in Android Marshmallow (API 23) or higher we need to ask runtime permissions.
To solve this issue use call :
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
in your onCreate() method
Thanks all for your help !

You can use
File sampleDir = new File(Environment.getExternalStorageDirectory(), "/test");
if (!sampleDir.exists()) {
sampleDir.mkdirs();
}

Related

MAUI: Update own application (Android)

How can I check my own app if there is a newer version available and then update itselfs automatically? The idea is to request an API for a new version and receives the apk file. Then the APK should be stored and the installation/update should start. I don't know how to create this. The request to the API is no big deal, but how can I store and execute the APK file on an Android device?
I only found this but there were too many compiling errors (Context, Java, Buld, Intent, FileProvider could not be found).
Can you gave me some hints how to solve this "problem"?
THANK YOU!
You can uninstall you app by the following code:
var name = this.ApplicationContext.PackageName;
Intent intent = new Intent(Intent.ActionDelete);
intent.SetData(Android.Net.Uri.Parse("package:" + name));
StartActivity(intent);
In addition, don't forget to add the permissions in the AndroidManifest.xml:
<uses-permission android:name="android.permission.DELETE_PACKAGES" />
<uses-permission android:name="android.permission.REQUEST_DELETE_PACKAGES" />
It is not necessary to uninstall the app. If it is build with the same keystore and signature, it is possible to install one version directly to overwrite the existing.
var context = Android.App.Application.Context;
var path = Path.Combine(Android.App.Application.Context.GetExternalFilesDir("").AbsolutePath, "com.companyname.Testproject.apk");
Java.IO.File file = new Java.IO.File(path);
using (Android.Content.Intent install = new Android.Content.Intent(Android.Content.Intent.ActionView))
{
Android.Net.Uri apkURI = AndroidX.Core.Content.FileProvider.GetUriForFile(context, context.ApplicationContext.PackageName + ".provider", file);
install.SetDataAndType(apkURI, "application/vnd.android.package-archive");
install.AddFlags(Android.Content.ActivityFlags.NewTask);
install.AddFlags(Android.Content.ActivityFlags.GrantReadUriPermission);
install.AddFlags(Android.Content.ActivityFlags.ClearTop);
install.PutExtra(Android.Content.Intent.ExtraNotUnknownSource, true);
Platform.CurrentActivity.StartActivity(install);
}
In the AndroidManifext.xml i added:
<uses-permission android:name="android.permission.INSTALL_PACKAGES" />
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
<uses-permission android:name="android.permission.REQUEST_DELETE_PACKAGES" />
<uses-permission android:name="android.permission.DELETE_PACKAGES" />

file.exists() return false

File oldFile = new File(dirPath+"/"+packageName+".apk");
Log.e("Path ",oldFile.getPath());
my log returns this:
E/Path:
/storage/emulated/0/Android/data/com.bazibaaz.app/files/com.YGD.GoldenBasket.apk
i have this file exactly on the path shown in the log, but this code:
oldFile.exists()
returns false!
i have permissions to read and write:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
here is the picture of file (to prove its existence):
( path: internal storage/storage/emulated/0/Android/data/com.bazibaaz.app/files/com.YGD.GoldenBasket.apk)

mkdir() or mkdirs() not working on Android API 26

Following code is working with other APIs but 26
public static boolean cretaeDir()
{
String dir = Environment.getExternalStoragePublicDirectory(Environment.MEDIA_SHARED).getAbsolutePath();
java.io.File folder = new java.io.File(dir + "/SomeFolderName");
if (!folder.exists()) {
try{
if(folder.mkdir()) {
Log.e("Creating Folder", "Success");
return true;
} else {
Log.e("Creating Folder", "Failed");
return false;
}
} catch(Exception e){
e.printStackTrace();
return false;
}
}else{
return true;
}
}
Before some one ask, Yeah the permissions have been granted and also uses-permission present in the Manifest file.
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.package.name">
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="com.android.vending.BILLING" />
<!-- Required OpenGL ES 2.0. for Maps V2 -->
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<application/>
Test Device is Samsung S8, Running 8.0.0
try this
File fsd = Environment.getExternalStorageDirectory();
String filePath = fsd.getAbsolutePath() + "/SomeFolderName";
File dir = new File(filePath);
if(dir.isDirectory==false || !dir.exists()){
dir.mkdirs();
}
ok, so this worked for me.
RequestPermission Methods, when asking for permission, ask WRITE_EXTERNAL_STORAGE before anything else. Try it...
MainActivity.kt
// First permission to be asked
requestPermissions(arrayOf(android.Manifest.permission.WRITE_EXTERNAL_STORAGE), 1)
// Then all other permissions
This is working for all API inclusive & levels above 26.
TBH, I don't know how, but it working.
You are not using a valid folder type. You have to choose from the following options
The type of storage directory to return. Should be one of DIRECTORY_MUSIC, DIRECTORY_PODCASTS, DIRECTORY_RINGTONES, DIRECTORY_ALARMS, DIRECTORY_NOTIFICATIONS, DIRECTORY_PICTURES, DIRECTORY_MOVIES, DIRECTORY_DOWNLOADS, DIRECTORY_DCIM, or DIRECTORY_DOCUMENTS. May not be null.
Your code should be something like this
String dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC).getAbsolutePath();
I have fixed it by the following way as I have no other option left and I have tried all the suggested solutions. Maybe the error is device dependent or whatever, but it has nothing to do with API 26 as I have upgraded one of my test devices to API 26 and the old function did worked.
The following code is working on all devices.
File file = MyApplication.getInstance().getDir("BUZZ",MODE_PRIVATE);
cacheDir = new File(file.getAbsoluteFile()+File.separator+IMAGE_DIRECTORY_NAME);
Happy Coding :-)

Android download manager copy file from cache

How to copy a file downloaded by download manager in internal storage's cache folder. I am using fileInputStream, but it through's permison denied. Is there any option to copy file
it has been a while since the question was asked but anyway, I found the answer this morning:
You simply can't copy files from you Caches folder. What you can do is to create your file in another folder (Files folder for example, which you get by doing getFilesDir()) and delete it if necessary !
Hope it helps,
Bye
Set permission in AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_DOWNLOAD_MANAGER" />

Permission denied Exception when creating new file

i want to create new file iam using this code
try {
File file = new File(Environment.getExternalStorageDirectory() + File.separator + "Contacts.vcf");
file.createNewFile();
} catch (IOException ioe)
{ioe.printStackTrace();}
and using this permission
<permission android:name="android.permission.MOUNT_FORMAT_FILESYSTEMS" ></permission>
but it keeps givinh me exeption that says Permission denied
you need to use this permission in manifest file
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
when you write any file in External Storage(SD CARD) or Internal Storage ,You have to add permission in manifest file...
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
add this permission into your manifest file
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
Give WRITE_EXTERNAL_STORAGE permission to your AndroidManifest file as shown below:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
.........
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
</manifest>

Categories

Resources