Android 11 : store file on app specific external storage - android-11

I wanted to store jpg file to app specific directory in external storage on android 11. Below code is working fine on emulator but not on google pixel 4a. can someone guide me here.
val appDirectory = context.getExternalFilesDir(null)
appDirectory?.let { dir ->
val file = File(dir.absolutePath, "test.jpg")
FileOutputStream(file).use {
imageFile.bitmap.compress(getBitmapFormat(imageFile.format), 100,it)
}
}

Related

How to open a link in smartwatch from phone

I would like to know how to open a link from a simple app that I have made to push a link from within the app from phone to open on the watch for example a link to google play store app to get downloaded on the smartwatch.
Thank you.
See the docs here https://developer.android.com/reference/androidx/wear/remote/interactions/RemoteActivityHelper
and an example here https://github.com/android/wear-os-samples/blob/d18c489ff415aa0fbb25c260e3aacdf50f7716e3/WearVerifyRemoteApp/Application/src/main/java/com/example/android/wearable/wear/wearverifyremoteapp/MainMobileActivity.kt
Log.d(TAG, "Number of nodes without app: " + nodesWithoutApp.size)
val intent = Intent(Intent.ACTION_VIEW)
.addCategory(Intent.CATEGORY_BROWSABLE)
.setData(Uri.parse(PLAY_STORE_APP_URI))
// In parallel, start remote activity requests for all wear devices that don't have the app installed yet.
nodesWithoutApp.forEach { node ->
lifecycleScope.launch {
try {
remoteActivityHelper
.startRemoteActivity(
targetIntent = intent,
targetNodeId = node.id
)
.await()

manually save jpg to emulator

We are dealing with the code from a course that loads an image. We are trying to do this with an Emulator API 26 Play Store active We have no jpg pictures on the emulator. The Device File Explore will let us Upload a jpg. We have tried various folders with no luck.
Our question is where to Upload the jpg to in the Device File Explore?
Code is Kotlin and the upload method is posted below
fun onChooseImage(view:View){
val intent = Intent()
intent.type = "image/*"
intent.action = Intent.ACTION_GET_CONTENT
val chooser = Intent.createChooser(intent,"Choose Image for Habit")
startActivityForResult(chooser,CHOOSE_IMAGE_REQUEST)
Log .d(TAG,"Image was sent" )
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if(requestCode == CHOOSE_IMAGE_REQUEST && resultCode == Activity.RESULT_OK
&& data != null && data.data != null){
Log.d(TAG,"An Image WAS Choosen")
val bitmap = tryReadBitmap(data.data)
bitmap?.let {
ivIcon.setImageBitmap(bitmap)
Log.d(TAG,"We Updated and Read Bitmap")
}
}
}
private fun tryReadBitmap(data: Uri?): Bitmap?{
return try{
MediaStore.Images.Media.getBitmap(contentResolver,data)
}catch (e:IOException){
e.printStackTrace()
null
}
}
And we are using Cold Boot on the emulator.
We have looked at other posts that suggest you can not use the emulator camera to save pictures to the emulator. Does this mean we need a real device to test this code?
Grendel's answer is workable but I would like to provide a less intensive set of steps
1. Load the app in question
2. Open AVD manager and select the little down icon next to the Emulator your using
3. Click on Wipe Data (suggestion keep Emulator setting Quick Boot) <- other topic
4. Store your jpg in this path Storage->Self->Primary-DCIM
5. This is done by RIGHT click DCIM select Upload navigate to jpg
6. Close AVD manage
7. Run your app and select the same Emulator
It seems with the emulator you need to scan the emulator with Dev Tools BUT one other issue seems to pop up the cache for google play services and google play store needs to be cleared. In the processes of fixing this we tried so many fixes not sure of all the steps in order. As for where to upload the jpg that we are sure of use this path with
/mnt/sdcard/DCIM/water.jpg When you get to DCIM right click it and select Upload
Here is a link that may help HELP LINK

Xamarin can not read the file

I'm trying to read a Json file for my mobile app in Xamarin Studio, but it throws me an error
can not find the file
While I try the same in Console Application every thing is all right.
I'm trying reading the file this way:
string Jason = File.ReadAllText(#"C:\Folder\file.txt")
This information is taken from Xam160 Class from Xamarin University. Within mobile application the platform generates a isolated folder area specifically for your App. These are the preferred locations to store files specific to your App with AppHome representing the Root of your sandboxed area.
Android - AppHome/files
iOS - AppHome/Lirbrary/[subdirectory]
Windows Phone - AppHome\Local
To Access these paths you can work with the Environment.SpecialFolder Enumeration to access these locations with the Environment.GetFolder() Method.
//Android
string path = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
//iOS
string docFolder = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
string libFolder = System.IO.Path.Combine(docFolder,"..","Library");
// Windows has different implementation. GetFolder Path with throw exception on windows.
string path = Windows.Storage.ApplicationData.Current.LocalFolder.Path;
Each of these will get you access to the path you need to be able to read and write files locally on which platform you are on.

Unity error while loading image from Windows Phone gallery

I have developped a Windows Phone plugin for my Unity application which enable me to open the Windows Phone gallery in order to select an image and get its physic path.
I've used this path to load the image in the application with a WWW object but I get this error
"Error. Operation has failed with error 0x80070005: access is denied."
The path is correct because I display it.
Here is my test code to load the image with the path.
IEnumerator loadImage()
{
this.guiText.text = filePath;
WWW www = new WWW(filePath);
yield return www;
if(www.error == null)
{
GameObject texture = GameObject.Find("UNITY");
if(texture != null)
{
texture.guiTexture.texture = www.texture;
}
}
}
Any help would be appreciated ! Thanks in advance !
I think you need to enable the permission for your application to give access to read files from the device.
Go to App Manifest file and check whether it is enabled or not, if not that will solve your problem.

How to store the Local image path in sqlite or xml database in Xamrin.Forms

I have given task to store the image path in database either xml schema or sqlite database.I am working on Xamarin.Forms PCL project. I am very confused with how to store image path in database.because in android image should be placed in Ressource/drawble,ios image placed in Resources folder. I read this xamarin.forms documentaion. But not getting how to get path and store in database. Please anyone provide any resources or example.
Hope you have gone through this documentation and to distinguish between iOS and Android is also Given here in this documentation .
if (Device.OS == TargetPlatform.iOS) {
// Save in sqlite with iOS path
}if (Device.OS == TargetPlatform.Android) {
// Save in sqlite with Android path
}
Hope it helps.

Resources