About Monkey 2 › Forums › Monkey 2 Programming Help › Android: How to save to Pictures folder
This topic contains 10 replies, has 4 voices, and was last updated by
Nalle 4 months, 1 week ago.
-
AuthorPosts
-
October 28, 2018 at 8:16 am #15545
Hello everyone,
How in Monkey2 the Pictures folder of Android systems can be retrieved?
I’m currently using path “sdcard/Pictures/”, but this probably isn’t the best way to do it.
I would like to have the pictures to be visible with computer too, so that it would be easy to transfer the files from Android device to computer.
The way I’m currently done it, the user has to copy or move first the file on Android device, which makes it visible to a computer too.
November 1, 2018 at 11:10 am #15550Hi. There is the function GetSpecialDir:String( name:String ) but it understand only “internal” and “external” params for android,
but you can extend it by changing this file
/monkey2/modules/std/filesystem/native/Monkey2FileSystem.java
add one more else branch:
Monkey1234567if( name.equals( "internal" ) ){f=Monkey2Activity.instance().getFilesDir();}else if( name.equals( "external" ) ){f=Monkey2Activity.instance().getExternalFilesDir( null );}else if( name.equals( "pictures" ) ){f=Environment.getExternalStoragePublicDirectory( Environment.DIRECTORY_PICTURES );}and rebuild std module for android target.
November 1, 2018 at 11:31 am #15551I will test and add realisation for all external dirs.
November 30, 2018 at 9:31 pm #15639With my new Samsung Galaxy A6+ saving to Pictures folder doesn’t work. I checked SavePixmap state from the debugger, it was false and nothing was saved. Has anyone else tried this?
December 1, 2018 at 1:28 am #15641It may be a permissions issue, try something like this…
RequestPermissions( New String[][(“android.permission.WRITE_EXTERNAL_STORAGE”),Lambda( results:UInt[] )
End )
I have no idea if this is the correct permission to ask for though, more here:
https://developer.android.com/reference/android/Manifest.permission
December 1, 2018 at 7:55 am #15644As default there is in the Android Manifest this permission: WRITE_EXTERNAL_STORAGE
I also thought, that could it be permissions issue, because ..Environment.getExternalStoragePublicDirectory( Environment.DIRECTORY_PICTURES ) is the correct way to get the Pictures folder.
December 1, 2018 at 10:27 am #15645if setting it to targetSdkVersion 21 works fine, then we have the same problem. working on it
December 1, 2018 at 10:34 am #15646this seem to be what has changed to increase the security in Android filesystem, i dont have any pacakaged solution yet as i dont have much time before Christmas. But it might give an idea to ppl.
https://developer.android.com/reference/android/support/v4/content/FileProvider
December 7, 2018 at 3:35 pm #15671From Android version 6 (API >= 23) one must ask at run-time permission for example to Pictures folder.
This was my first try to do Android coding inside the Android Studio and I came up with this not working skeleton, that is based on Google’s example code:
Java123456789101112131415161718192021222324252627else if( name.equals( "pictures" ) ){if (ContextCompat.checkSelfPermission(Monkey2Activity.instance(),Manifest.permission.READ_EXTERNAL_STORAGE)!= PackageManager.PERMISSION_GRANTED) {// Permission is not granted// Should we show an explanation?if (ActivityCompat.shouldShowRequestPermissionRationale(Monkey2Activity.instance(),Manifest.permission.READ_EXTERNAL_STORAGE)) {// Show an explanation to the user *asynchronously* -- don't block// this thread waiting for the user's response! After the user// sees the explanation, try again to request the permission.} else {// No explanation needed; request the permissionActivityCompat.requestPermissions(Monkey2Activity.instance(),new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE);// MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE is an// app-defined int constant. The callback method gets the// result of the request.}} else {// Permission has already been grantedf = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);}Edit: Also this in addition:
Java1234567891011121314151617181920public void onRequestPermissionsResult(int requestCode,String permissions[], int[] grantResults) {switch (requestCode) {case MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE: {// If request is cancelled, the result arrays are empty.if (grantResults.length > 0&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {// permission was granted, yay! Do the// READ_EXTERNAL_STORAGE-related task you need to do.} else {// permission denied, boo! Disable the// functionality that depends on this permission.}return;}// other 'case' lines to check for other// permissions this app might request.}}Has anyone a working solution?
Working solution should ask the user, if the user agrees to app’s writing to external storage.
edit: Link to original code: https://developer.android.com/training/permissions/requesting#java
December 7, 2018 at 4:48 pm #15672I made a new code, this little code (needs some improvement) seems to work.
It prompts the user (system feature) if the app may write to external storage, after permission, when the permission is granted, the picture is saved to Pictures folder!
Java123456789101112131415161718192021222324252627282930313233343536373839if( name.equals( "internal" ) ){f=Monkey2Activity.instance().getFilesDir();}else if( name.equals( "external" ) ){f=Monkey2Activity.instance().getExternalFilesDir( null );}else if( name.equals( "pictures" ) ){String[] permissions = {Manifest.permission.WRITE_EXTERNAL_STORAGE};com.monkey2.lib.Monkey2Permissions.requestPermissions(permissions, WRITE_REQUEST_CODE);f= Environment.getExternalStoragePublicDirectory( Environment.DIRECTORY_PICTURES );}if( f!=null ) return f.getAbsolutePath()+"/";return "";}public static String onRequestPermissionsResult(int requestCode,String permissions[], int[] grantResults) {switch (requestCode) {case WRITE_REQUEST_CODE: {// If request is cancelled, the result arrays are empty.if (grantResults.length > 0&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {// permission was granted, yay! Do the// read external storage-related task you need to do.return Environment.getExternalStoragePublicDirectory( Environment.DIRECTORY_PICTURES ).getAbsolutePath()+"/";} else {// permission denied, boo! Disable the// functionality that depends on this permission.return "NO_PERMISSION";}}// other 'case' lines to check for other// permissions this app might request.}return null;}We should add proper code to Monkey2 modules to get more Android features like Camera.
December 7, 2018 at 10:20 pm #15674I found this old thread: http://monkeycoder.co.nz/forums/topic/android-permissions/
The permissions can be asked simply in nice Monkey2 code.
Though, I had at first problem with the following line:
RequestPermissions(permissions,Lambda( results:Int[] )
Error : Can’t find overload for ‘RequestPermissions’ with argument types (monkey.types.String[],Void(monkey.types.Int[]))
If it is changed to: RequestPermissions( permissions,Lambda( results:ResultType[] ) it can be compiled.
-
AuthorPosts
You must be logged in to reply to this topic.