Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Create new folder inside the device
Started by pabloapdz Jul 10 2018 08:06 AM

20 replies to this topic
[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

pabloapdz

[GLOBAL: userInfoPane.html]
pabloapdz
  • Contributor

  • 113 posts
  • Corona SDK

Hi !

 

I was wanting to create a new pasture inside the user's device, that is, inside the root folder, but it does not seem to me that anyone has tried this before, so I tried to do the following:

 

First, I thought the following: "If I want to create a folder inside the root folder,

I will need to know the root folder path first," then I found that there is a pattern

and the path is: "/storage/emulated/0/"

 

Then, i tried make this:

local lfs = require( "lfs" )


-- Get raw path to the app temporary directory
local temp_path = system.pathForFile( "", "/storage/emulated/0/" )


-- Change current working directory
local success = lfs.chdir( temp_path )  --returns true on success
local new_folder_path


if ( success ) then
lfs.mkdir( "NovaPasta" )
    new_folder_path = lfs.currentdir() .. "/NovaPasta"
end
 

* link of api

https://docs.coronalabs.com/api/library/lfs/index.html

 

But as I already imagined, it did not work .. Does anyone know how I can create a new folder inside the root folder of the device? (The folder that I call root folder, is the one in which you have all the folders of the phone, the Whatsapp folder, Download, etc ...)



[TOPIC: post.html]
#2

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 6,978 posts
  • Corona SDK

Resource Directory

You can't modify the resource directory because:

 

1. On Android it is not an actual folder.

 

2. All mobile OSes lock that folder if it exits at all as a folder. i.e. You can't create sub-folders in the Resource Directory on iOS.

 

Other Directories

You can modify Documents, Temporary, etc.

 

Outside The Sandbox

With regards to making files and folders outside the sandbox area of your app.  Unless the device is rooted you probably can't do that.

 

However, there may be some plugins to help with this in the marketplace: https://marketplace.coronalabs.com/



[TOPIC: post.html]
#3

StarCrunch

[GLOBAL: userInfoPane.html]
StarCrunch
  • Contributor

  • 751 posts
  • Corona SDK

Maybe this one?



[TOPIC: post.html]
#4

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 6,978 posts
  • Corona SDK

One more note.  If you do work out the paths and permissions aspect of this, SSK comes with a library for maniuplating files and folders in any OS:

 

https://roaminggamer.github.io/RGDocs/pages/SSK2/libraries/files/

 

For example, if you knew that this folder existed and was writeable: '/storage/emulated/0/'

 

You could make a sub-folder named 'bob' in it like this:

ssk.files.util.mkFolder( '/storage/emulated/0/bob' )


[TOPIC: post.html]
#5

pabloapdz

[GLOBAL: userInfoPane.html]
pabloapdz
  • Contributor

  • 113 posts
  • Corona SDK

I do not understand why it does not work ... I tried the 2 ways presented here, and each one individually did not work. Soon I tried to use SSK and do the following:

local output = "Date" .. ";" .. "Hour" .. ";" .. "Selection" .. "\n" 

require "ssk2.loadSSK"
_G.ssk.init( { exportSystem = true } )
io.appendFile( output, "testeF.csv" )  
----------------------------------------------------------------
local function listener (event)

    if event.action == "sent" then
        print ("file was sent ok")
    elseif event.action == "cancelled" then
        print ("file was not sent")
    end

end

local library = require "plugin.cnkFileManager"


library.copyFileToExternalStorage (
{
listener = listener,
mainFolder = "FolderOne",
subFolder = "FolderTwo",
newFileName = "testeF.csv",
isPublic = true,
isRemovable = false,
file = 
{
    {filename = "testeF.csv", baseDir = system.ResourceDirectory},
},
})

But nothing happens, no folder is created or anything!

Does anyone know what's wrong?

 

*My build.settings:

settings =
{
	orientation =
	{
		-- Supported values for orientation:
		-- portrait, portraitUpsideDown, landscapeLeft, landscapeRight
		default = "portrait",
		supported = { "portrait", },
	},

	--
	-- Android section
	--
	android =
	{
		usesPermissions =
		{
			"android.permission.INTERNET",
            "android.permission.ACCESS_FINE_LOCATION",
            "android.permission.ACCESS_COARSE_LOCATION",	
			"android.permission.WRITE_EXTERNAL_STORAGE",
			"android.permission.INSTALL_PACKAGES",
			"android.permission.READ_EXTERNAL_STORAGE",   
		},
	},

	--
	-- iOS section
	--
	iphone =
	{
		xcassets = "Images.xcassets",
		plist =
		{
			UIStatusBarHidden = false,
			UILaunchStoryboardName = "LaunchScreen",
		},
	},

	--
	-- Plugins section
	--
	plugins =
	{
		['plugin.cnkFileManager'] = {publisherId = 'es.cnksoft'},
	},

	--
	-- Project section
	--
	excludeFiles =
	{
		-- Exclude unnecessary files for each platform
		all = { "Icon.png", "Icon-*dpi.png", "Images.xcassets", },
		android = { "LaunchScreen.storyboardc", },
	},
}



[TOPIC: post.html]
#6

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 6,978 posts
  • Corona SDK

My guess (as I said above) is that you don't have permission to write in that folder.

 

Also, are you absolutely sure you have the right path?

 

 

 

Regarding your mention of SSK.

 

As far as I can tell from your code, the only SSK feature you're using is the io extension to write/append files

 

You are appending to a file in the system.DocumentsDirectory

io.appendFile( output, "testeF.csv" )  

You are not (in that code) using the files library.



[TOPIC: post.html]
#7

pabloapdz

[GLOBAL: userInfoPane.html]
pabloapdz
  • Contributor

  • 113 posts
  • Corona SDK

My guess (as I said above) is that you don't have permission to write in that folder.

 

Also, are you absolutely sure you have the right path?

 

 

 

Regarding your mention of SSK.

 

As far as I can tell from your code, the only SSK feature you're using is the io extension to write/append files

 

You are appending to a file in the system.DocumentsDirectory

io.appendFile( output, "testeF.csv" )  

You are not (in that code) using the files library.

 

 
But the intention is just to save in Documents, and then use the library.copyFileToExternalStorage to move the file .. That's neither in the code :(
 
 
-- edit

even with you putting in bold I could not see where the error is, because I'm actually using the ".library .." = c



[TOPIC: post.html]
#8

pabloapdz

[GLOBAL: userInfoPane.html]
pabloapdz
  • Contributor

  • 113 posts
  • Corona SDK

Can someone help me ? I have not found the answer so far.



[TOPIC: post.html]
#9

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 6,978 posts
  • Corona SDK

Hi.  I think part of the reason nobody is helping is others (like myself) may have a lot of questions about what you want to achieve and why. 

 

Additionally, AFAIK, there is not a lot of uniformity in the file structure of Android file-systems on different manufactures' devices. i.e. Different devices may have different folder paths to similar storage.  This means, coming up with a universal solution may not work in all cases, thus helping write one may lead to you saying, "it doesn't work on my device." 

 

I can tell you from experience, nobody who spends time helping by writing a sample wants this to be the end result and response.

 

 

So, let me ask some questions and hope that the answers intrigue folks enough for some help to be forthcoming.

 

1. Why do you want to do this?  What purpose does it serve?  What requirement of your app does it resolve?  

 

On first glance, it seems like a very odd thing to do and a little sketchy.   I don't want to be unkind, but I'm a little hesitant to help write code that can write outside the sandbox and essentially bypass the security of Android.

 

That said, I'm sure there are many perfectly reasonable uses of this.  However, knowing what you want to do, there may be safer and better solutions folks can suggest.

 

 

2. What are the make, model, and Android version of the device(s) you are testing on?  Knowing this may help.

 

 

3. Would you consider making, zipping up, then sharing a functional micro project that attempts to do this so folks can directly examine your code? 

 

The only way to solve this problem is by having a real project, so asking for help is essentially asking us to code up a project for you to look at.

 

I know that I personally, in light of the other challenges of this question I outlined above, can't spend time on this.  

 

 

I hope you get help on this, but you may need to make it easier for folks to pitch in and to debug/resolve this.

 

Cheers,

Ed


Edited by roaminggamer, 11 July 2018 - 09:52 AM.


[TOPIC: post.html]
#10

pabloapdz

[GLOBAL: userInfoPane.html]
pabloapdz
  • Contributor

  • 113 posts
  • Corona SDK

Sorry for the delay, I was in college.

1-

And well, I did not really know that you had this concern if the person will use it for good or ill, I will explain what I am doing:

 

I have an app that makes it easier for people to fill in boring and repetitive tasks from day to day, after completing everything, this application releases me a .CSV file. Only, let's agree on one thing. If you put your device on the computer, you want to be easy to find these tables, right? And to get a more beautiful and more professional thing, I'd need a folder inside the root folder. As if it were whatsapp, it only thinks if whatsapp did not create folders, just threw everything there and ready, it would be a terrible mess.

So I want to do just that, I want to create a folder inside the root folder, called "TABLES APP X" and inside this folder, will have a subfolder separating the days. and within these folders of the day, will have the tables in .csv

 

2 -

 

MOTO G PLAY, android 7.1.1

 

3 -

 

I did not exactly understand this item :(

 

One question, would you advise me to delete this topic and open a new one explaining these 2 items?



[TOPIC: post.html]
#11

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 6,978 posts
  • Corona SDK

#3 - Make a sample project that is very very small showing us what you're trying to do in terms of creating a folder and writing to it.  Then zip the project and attach it to this post.

 

 

Question: Is your device rooted?  If not, I really don't think Android will let you write to the root folder.

 

https://www.quora.com/Where-is-the-root-directory-in-android-mobile-phone

 

 

No need to start a new thread. :)



[TOPIC: post.html]
#12

pabloapdz

[GLOBAL: userInfoPane.html]
pabloapdz
  • Contributor

  • 113 posts
  • Corona SDK

sky roaminggamer! (Brazilian expression, that is to say: My God!)

It's not in the root folder of the device, but in the INNER folder, the root is a totally different thing. Now I understand the reason for question # 1.

Very sorry ! I meant "How to create a new folder in the device's internal folder"

 

very very very very sorry !

 

 

Thanks for the link, if not I would be thinking by now that the "root folder" was the "internal folder"



[TOPIC: post.html]
#13

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 6,978 posts
  • Corona SDK

I did some experiments but didn't really get it working.
 
At the end I came up with this:
require "ssk2.loadSSK"
_G.ssk.init( { measure = false } )

local files = ssk.files

files.util.mkFolder('/storage/emulated/0/bubba')

local src = files.resource.getPath("test.csv")
local dst = '/storage/emulated/0/bubba/test.csv'
files.util.cpFile( src, dst )


Here is my test app: https://github.com/roaminggamer/RG_FreeStuff/raw/master/AskEd/2018/07/androidFolderHunting.zip
 
Here are my results:
1.png
2.png
3.png



PS - It is worth noting, files did not show up right away. There was a several second delay before I could see them from my cable-attached PC.

[TOPIC: post.html]
#14

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 6,978 posts
  • Corona SDK

Yeah, you need to be clear with the words you use.

 

I know there is a language barrier, but these are technical terms that should be universal across all languages since they are newcomers to the language scene.  

 

(If I'm wrong in this, someone please correct me, but in my experience living and working in foreign countries, other technical Unix/Linux folks knew what I meant when I said 'root').

 

Since we're talking about Android, I have to assume you know a little bit about Linux and Android.  If not, then I understand the confusion.

Note: Your use of the term 'internal folder' has no meaning to me.  I think perhaps, you mean the 'internal storage'.  

Also, and again, in my experience Android folder systems and hierarchies are NOT consistent so whatever solution you may come up with is simply not going to work universally.

You're better off coming up with something that works universally, like writing to an Amazon S3 bucket or something like that.



[TOPIC: post.html]
#15

pabloapdz

[GLOBAL: userInfoPane.html]
pabloapdz
  • Contributor

  • 113 posts
  • Corona SDK

I did some experiments but didn't really get it working.
 
At the end I came up with this:

require "ssk2.loadSSK"
_G.ssk.init( { measure = false } )

local files = ssk.files

files.util.mkFolder('/storage/emulated/0/bubba')

local src = files.resource.getPath("test.csv")
local dst = '/storage/emulated/0/bubba/test.csv'
files.util.cpFile( src, dst )


Here is my test app: https://github.com/roaminggamer/RG_FreeStuff/raw/master/AskEd/2018/07/androidFolderHunting.zip
 
Here are my results:
1.png
2.png
3.png



PS - It is worth noting, files did not show up right away. There was a several second delay before I could see them from my cable-attached PC.

 

 

I tried to put this code in the code of my project and it did not work, did not create the folder or anything, I was confused and then I downloaded the example you posted, I created the APK and downloaded it on my cell phone, then gave the following error:

Runtime Error:
   ssk2 \ files \ RGFiles_util.lua: 337: can not open /: Permission denied.

(Note: I have agreed the permission for internal storage)

 

 

Yeah, you need to be clear with the words you use.

 

I know there is a language barrier, but these are technical terms that should be universal across all languages since they are newcomers to the language scene.  

 

(If I'm wrong in this, someone please correct me, but in my experience living and working in foreign countries, other technical Unix/Linux folks knew what I meant when I said 'root').

 

Since we're talking about Android, I have to assume you know a little bit about Linux and Android.  If not, then I understand the confusion.

Note: Your use of the term 'internal folder' has no meaning to me.  I think perhaps, you mean the 'internal storage'.  

Also, and again, in my experience Android folder systems and hierarchies are NOT consistent so whatever solution you may come up with is simply not going to work universally.

You're better off coming up with something that works universally, like writing to an Amazon S3 bucket or something like that.

 

I understand, I will be more careful about the words, I hate to use google translator because it does not translate 100% .. Here in Brazil, "Internal folder" and "Internal storage" have the same meaning .. Now about root, mine, I confused.



[TOPIC: post.html]
#16

pabloapdz

[GLOBAL: userInfoPane.html]
pabloapdz
  • Contributor

  • 113 posts
  • Corona SDK

MY GOD, how can it be so hard to write a new folder in the internal android storage ?? I can not imagine why this is practically impossible, I have my head to explode already !!!



[TOPIC: post.html]
#17

Rob Miracle

[GLOBAL: userInfoPane.html]
Rob Miracle
  • Moderator

  • 24,569 posts
  • Corona Staff

Creating folders complexity varies depending on where you're trying to create them.

 

Just to clear the language up and make sure we are all talking about the same thing:

 

root - This is the top-level folder on a device. Unless the device has been hacked (rooted/jailbroken) you cannot write to any arbitrary folder.

app sandbox - This is what I think of when you say "internal folders". Corona makes five core locations in the sandbox available to you:

 

system.ResourceDirectory - Where your app's code, images, and other resources live. You cannot write here. It's read-only.

system.DocumentsDirectory - You can write files here including making directories. This is where you should store files created by users as well.

system.TemporaryDirectory - A place where you can write files that you have no intention of them lasting for ever.

system.CachesDirectory - A place where you can download files from the Internet to. It's good for content that is easily replaceable. There is no guarantee these files have permanence either. 

system.ApplicationSupportDirectory - A place to store configuration information and other files the app needs that's not necessarily user data. 

 

See:  http://docs.coronalabs.com/api/library/system/index.html#constants

 

External storage: This is an Android only feature where your device lets you write files too that was originally on the device's SD card (which on many Android devices is removable). On Android, this is where music, photos, and other large volume data files go. Corona does not officially support External storage. There are a couple of plugins that help here.  If you can safely determine the path to external storage then you can create directories here and write files here as well.

 

Corona makes the lfs.* library available to you which you can use to make directories in any folder you that have write access to.

 

Rob



[TOPIC: post.html]
#18

pabloapdz

[GLOBAL: userInfoPane.html]
pabloapdz
  • Contributor

  • 113 posts
  • Corona SDK

Creating folders complexity varies depending on where you're trying to create them.

 

Just to clear the language up and make sure we are all talking about the same thing:

 

root - This is the top-level folder on a device. Unless the device has been hacked (rooted/jailbroken) you cannot write to any arbitrary folder.

app sandbox - This is what I think of when you say "internal folders". Corona makes five core locations in the sandbox available to you:

 

system.ResourceDirectory - Where your app's code, images, and other resources live. You cannot write here. It's read-only.

system.DocumentsDirectory - You can write files here including making directories. This is where you should store files created by users as well.

system.TemporaryDirectory - A place where you can write files that you have no intention of them lasting for ever.

system.CachesDirectory - A place where you can download files from the Internet to. It's good for content that is easily replaceable. There is no guarantee these files have permanence either. 

system.ApplicationSupportDirectory - A place to store configuration information and other files the app needs that's not necessarily user data. 

 

See:  http://docs.coronalabs.com/api/library/system/index.html#constants

 

External storage: This is an Android only feature where your device lets you write files too that was originally on the device's SD card (which on many Android devices is removable). On Android, this is where music, photos, and other large volume data files go. Corona does not officially support External storage. There are a couple of plugins that help here.  If you can safely determine the path to external storage then you can create directories here and write files here as well.

 

Corona makes the lfs.* library available to you which you can use to make directories in any folder you that have write access to.

 

Rob

Rob, what I want is to create a new folder where you can even create a new folder by your own device, keeping that in mind, I think I'm just searching in the wrong way ...

I want to create a new folder in this place here (I'll explain how to get there because it was too confusing from here already kk)

 

Settings -> Storage -> Explore (it's the last option)

 

Just this, I just need to create a folder there, but to have this whole job, I know it must be VERY simple just because there is an option in the device itself, but until now I have not been able to do this)

 

Forget the root folder, I do not want to create anything there, it was just a misconception.

 

I've given the idea of ​​moving an application sandbox file over there because I'm not sure if I can create direct .csv in this folder. If you could create it there at once it would be much easier and faster



[TOPIC: post.html]
#19

Rob Miracle

[GLOBAL: userInfoPane.html]
Rob Miracle
  • Moderator

  • 24,569 posts
  • Corona Staff

Looking at my Nexus 9, it appears that is either "External Storage" or something that blends external storage with some access to sandbox folders.

 

You can look at these plugins:

 

https://marketplace.coronalabs.com/corona-plugins/external-storage

https://marketplace.coronalabs.com/corona-plugins/android-file-and-uri-manager

 

You should be able to get the path to external storage from these then use lfs.mkdir() to create a subfolder.

 

Rob



[TOPIC: post.html]
#20

pabloapdz

[GLOBAL: userInfoPane.html]
pabloapdz
  • Contributor

  • 113 posts
  • Corona SDK

Many thanks Rob!

 

But I still persist in doubt, the way:

 

settings -> storage -> explore

 

is it called "external storage"?

Did you think it was "internal storage"?



[TOPIC: post.html]
#21

Rob Miracle

[GLOBAL: userInfoPane.html]
Rob Miracle
  • Moderator

  • 24,569 posts
  • Corona Staff

When I look at my Nexus 9 (Android 7.1.1), I see folders like Download, Movies, Music, Pictures. These are not app sandbox folders. Therefore it has to be external storage.

 

Rob




[topic_controls]
[/topic_controls]