Write a ByteArrayOutputStream as a file to data/data/packagename/files - android

I have compactFile: ByteArrayOutputStream() and I want to create a file in the directory
data/data/package.name/files
So what I do is inside a Workmanager
Application.instance.openFileOutput(localUriName, Context.MODE_PRIVATE).use {
it.write(compactFile.toByteArray())
}
The compactFile can be a an Image or a Video or a Document or whatever.
However, sometimes the file never gets saved in the path. And I want to understand why is this happening. Is there a size limitation issue? Is it because I don't do it right..? Any ideas?

Related

Unable to find saved image

I'm very new to Android. I'm working with Xamarin and I have to take a picture with camera and save the picture.
I achieved to take the picture, I have a Bitmap object. Then I save it without error but when I try to find it, there is no file.
There is my code :
Bitmap imgBmp = /* image initialized */
//Save image on folder
var folderPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
var filePath = System.IO.Path.Combine(folderPath, "image1.png");
var stream = new System.IO.FileStream(filePath, FileMode.Create);
bool isOK = imgBmp.Compress(Bitmap.CompressFormat.Jpeg, 95, stream);
stream.Flush();
stream.Close();
I have no error when execute, isOK is true but when I search for the image.png I'm unable to find the file.
With debugger I saw that the path is : /data/user/0/com.myCompagny.MyAppli/files/image1.png but I can not see that folder.
Can someone help me to find my image1.png ?
Or to change the default folder to something like Pictures\MyApplication\image.png but I don't know how to find default folder for images.
Your file is there, but you have no permissions to see it. App directories are only readable to the owning app's uid. If you try to find your file through the shell, your uid is different.
You should try to use another folder path if you want to save file in a world readable location.
I'm just guessing but maybe System.Environment.SpecialFolder.CommonPictures would do.

How can I export Layout or Views as images to visible gallery folder in Android?

After searching about "How to save Layout views as images", I've found some solution to save in Internal and External Storage. But It seems the image file created is going to save in some data/data/... folder that is not visible normally. Actually I want the image visible in gallery for the user. I've found some code like below, but I even can't check if the image is created or not:
View content = findViewById(R.id.relativeLayout);
String yourimagename = "MyImageFile";
content.setDrawingCacheEnabled(true);
Bitmap bitmap = content.getDrawingCache();
File file = new File("/" + yourimagename + ".png");
try {
if (!file.exists()) {
file.createNewFile();
}
FileOutputStream ostream = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.PNG, 10, ostream);
ostream.close();
content.invalidate();
} catch (Exception e) {
e.printStackTrace();
} finally {
content.setDrawingCacheEnabled(false);
}
But It seems the image file created is going to save in some data/data/... folder that is not visible normally.
The file will be saved where the programmer elects to save it.
Actually I want the image visible in gallery for the user. I've found some code like below, but I even can't check if the image is created or not
That code will not work on any version of Android, as new File("/" + yourimagename + ".png") is not going to give you a usable File, as it points to a place that you can neither read nor write.
You are welcome to save the image to internal storage or external storage. Since you want this image to be picked up by "gallery"-type apps, you are best off choosing external storage, then using MediaScannerConnection and its scanFile() method to get the file indexed by the MediaStore, since gallery apps will tend to use the MediaStore as their source of images.
On the whole, I worry that getDrawingCache() will be unreliable. You may be better served telling your root View to draw to your own Bitmap-backed Canvas instead.

Android create file without saving

File tempFile = File.createTempFile(""+Common.getMobileNumber(), ".jpg", null);
FileOutputStream fos = new FileOutputStream(tempFile);
fos.write(bos.toByteArray());
I do this, but thing is ...when i go to apps, and click on my app... i see that its adding caching. and this particular file is caching, how can i create file without caching/saving.
only need of file for me, is to send it over to server side. Is there any way i create a object file without saving/caching?
If you are saying you need like a file only in the memory for the particular task like uploading to server. It is very similar to this question to do that. There is a code to create a file in memory.
Android Creating a memory resident input file that can be attached to an email

ANDROID internal storage file delete/exist not working for me

First of all, I want to know if making a file without extension is okay. For example, making a file with ".txt" extendsion will make it a txt file on a computer, but I don't know it matters in android. And I noticed that when working on eclipse, I could deleted a fild with the extensions but couldn't delete a file without a extension.
I want my program to delete a file in the internal storage (com.name.application folder) but it's not working for me.
I make a file with
FileOutputStream fos = openFileOutput(FileName, MODE_PRIVATE);
write with
bos = new BufferedOutpuStream(openFileOutput(FileName,Context.MODE_APPEND)
bos.write(newVocabFile.getBytes());
and I want to delete with
FILE file = new File(fileName);
fild.delete();
I did researches on google and applied different methods to my codes, but every method did not work for me. Because .delete() does not work properly, .exist() does not work either. I tired making the mile with and without extension but both ways did not work either.
I really need to get through this in order to finish my application. Please help me
You have a typo on this line
FILE file = new File(fileName);
fild.delete();
It should be
File file = new File(fileName);
file.delete();

FileOutputStream append on SD

I have a problem till now I can not understand.
os = new FileOutputStream(file,doAppend);
The file is on SD. If doAppend is false the file is saved correctly. If I split file in blocks and use first write with doAppend=false and successive with true the file is not saved correctly (as it is truncated).
The same function saves also to private folder on device but uses just os = activity.openFileOutput(red.m_slika_ime, Context.MODE_PRIVATE+Context.MODE_APPEND);
Between saving to SD or private the only difference is in OutputStream instantiation.
Since on private folder all works as expected it should be the FileOutputStream with append on Sd that is not working correctly.
Is this a known issue? workaround?
Thanks
It was my stupid error. I was elsewhere, each time before writing to SD, erasing my own file.
Happens:)
Thanks Luke anyway

Categories

Resources