png transparent image file in android - android

I want to save a png image to SD card and it should be without background.
in the sd card I do not see any background but when attach it into telegram I see a white background. here is my code in android:
File filepath = Environment.getExternalStorageDirectory();
File dir = new File(filepath.getAbsolutePath() + "/background_eraser/");
dir.mkdirs();
mImagename = "image" + cal.getTimeInMillis() + ".png";
file = new File(dir, mImagename);

Related

Save Converted PDF Directly to Server not in Device memory

I want to save my file directly into the server and not into my device. currently, the converted PDF file is being saved into my phone's storage. how do i get it to send that file into the server directly?
the path that i want to save the file to is:
"http://"+IP+"/OMAS/docs/"
my code that contains the path that saves into the internal storage:
File folder = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/PDFfiles/");
boolean success = true;
if (!folder.exists()) {
success = folder.mkdir();
}
path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/PDFfiles/";
File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/PDFfiles/");
path = path + filename + ".pdf";
Document document = new Document(PageSize.A4, 38, 38, 50, 38);
Rectangle documentRect = document.getPageSize();
try {
PdfWriter.getInstance(document,new FileOutputStream(path));

Android save photo on disk in private mode

I'm able to save a Image in disk using :
String baseDir = Environment.getExternalStorageDirectory().getAbsolutePath();
String fileName = imageId + ".png";
String filePath = baseDir + File.separator + fileName;
File file = new File(filePath);
if(!file.exists())
{
out = new FileOutputStream(baseDir + File.separator + fileName);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
}
But all the images saved are visible in the gallery of the phone .. How can I hide them to the user ? Is there a private folder for my appliaction ?
Thanks
Save it to the internal storage using Context.getFilesDir() instead of Environment.getExternalStorageDirectory().
From the docs:
Files saved here are accessible by only your app by default.
Check this link out.
public File getAlbumStorageDir(Context context, String albumName) {
// Get the directory for the app's private pictures directory.
File file = new File(context.getExternalFilesDir(
Environment.DIRECTORY_PICTURES), albumName);
if (!file.mkdirs()) {
Log.e(LOG_TAG, "Directory not created");
}
return file;
}
I think this is what you want. Read https://developer.android.com/training/basics/data-storage/files.html#WriteExternalStorage for more information.

Creating directory hierarchy in Android (for gallery)

I have some code like this:
photo = new File(android.os.Environment.getExternalStorageDirectory(), "images/mydir/" + File.separator + timeStamp + ".jpg");
photo.getParentFile().mkdirs();
photo.createNewFile();
But in the gallery, I'm only seeing "mydir" with the photos inside it. No "images" directory to be found.
How do I create a hierarchy so there's an image folder and mydir is inside that?
Thanks
You have problem in your code as you have one / after mydir and you are also adding File.separator so final output will be images/mydir//
photo = new File(android.os.Environment.getExternalStorageDirectory(), "images/mydir/" + File.separator + timeStamp + ".jpg");
Just remove the / after mydir like below
photo = new File(android.os.Environment.getExternalStorageDirectory(), "/images/mydir" + File.separator + timeStamp + ".jpg");
This should work now.

Android file exists problems

When i make File file = new File(etc..) i actuly created that file on SD card? Iam bit confused because everytime my condition is true and program jumps in if tree but in that time there is no file on SD card...
String filename = "pictures.data";
String root = Environment.getExternalStorageDirectory().toString();
File dir = new File(root + "/courier/saved/");
File file = new File(dir,filename);
if (file.exists())
// program jumps here
else{
}
OK so you can see the file in a dir /courier/saved/. Anyway here's my answer
String filename = "pictures.data";
String root = Environment.getExternalStorageDirectory().toString();
File dir = new File(root + "/courier/saved/");
File file = new File(dir,filename);
file.mkdirs();
if (file.exists())
// program now stops here
else{
}
Best would be to create the dir beforehand maybe, But this creates it on the fly

Android: how to set the path for saving files etc like in windows we write c:/someting/...what will I do in android

I am trying to record voice and save it in a file. For this I have to give a path to save the file, but I don't know how will I set the path...I have recently started working on android phone. In windows we set path like a drive e.g. C:/folder/folder....in android phone what will be my root? Where can I save an audio file? Write now I am working on emulator...Will the path be same for both emulator and phone?
we can create file like this in SD card
String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/folder");
myDir.mkdirs();
String fname = "file";
File file = new File (myDir, fname);
file is the path of file where you stored.
and add this Permission in manifest
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
_path=Environment.getExternalStorageDirectory().getPath() + "/RECORDS/";
// || ||
// VV VV
// storage path folder name
fileName = String.format("filename.mp3");
File file = new File(_path, fileName);
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
Save your file in Sdcard.
File path = Environment.getExternalStorageDirectory();
String cardName = path.getName();
path=cardName+"/mypathname/file_name.mp3";
It is also possible in emulator. create sdcard , when you create emulator.
Thanks
Uri outputFileUri;
File root = new File(Environment.getExternalStorageDirectory()
+ File.separator + "myDir" + File.separator);
root.mkdirs();
sdImageMainDirectory = new Filenter code here(root, "myPicName.jpg");
outputFileUri = Uri.fromFile(sdImageMainDirectory);

Categories

Resources