Take photo save it and get photo in Android [duplicate] - android

My app should save files to a place where, when you connect your phone/tablet to a computer, you can see them through the system file explorer.
This is the way I implemented file writing:
protected String mDir = Environment.DIRECTORY_DOCUMENTS;
protected File mPath = Environment.getExternalStoragePublicDirectory(mDir);
protected void writeLogFile(String filename) {
File f = new File(mPath, filename + ".txt");
f.getParentFile().mkdirs();
try (BufferedWriter bw = new BufferedWriter(new FileWriter(f, false))) {
// Details omitted.
} catch (Exception e) {
e.printStackTrace();
return;
}
makeText("Wrote " + f.getAbsolutePath());
}
This is what I see when I connect my Sony Xperia Z4 tablet to Windows (notice missing documents folder):
This is the directory to which the file is written (using above implementation):
What is wrong with my implementation?

What is wrong with my implementation?
MediaStore has not discovered your newly-created files yet. What you see in Windows — and in many on-device "gallery" apps — is based on what MediaStore has indexed.
Use MediaScannerConnection and its scanFile() method to tell MediaStore about your file, once you have written out your data to disk:
public void scanFile(Context ctxt, File f, String mimeType) {
MediaScannerConnection
.scanFile(ctxt, new String[] {f.getAbsolutePath()},
new String[] {mimeType}, null);
}
or, in Kotlin:
fun scanFile(ctxt: Context, f: File, mimeType: String) {
MediaScannerConnection.scanFile(ctxt, arrayOf(f.getAbsolutePath()), arrayOf(mimeType), null)
}

Related

Unable to pick audio file from storage [duplicate]

My app should save files to a place where, when you connect your phone/tablet to a computer, you can see them through the system file explorer.
This is the way I implemented file writing:
protected String mDir = Environment.DIRECTORY_DOCUMENTS;
protected File mPath = Environment.getExternalStoragePublicDirectory(mDir);
protected void writeLogFile(String filename) {
File f = new File(mPath, filename + ".txt");
f.getParentFile().mkdirs();
try (BufferedWriter bw = new BufferedWriter(new FileWriter(f, false))) {
// Details omitted.
} catch (Exception e) {
e.printStackTrace();
return;
}
makeText("Wrote " + f.getAbsolutePath());
}
This is what I see when I connect my Sony Xperia Z4 tablet to Windows (notice missing documents folder):
This is the directory to which the file is written (using above implementation):
What is wrong with my implementation?
What is wrong with my implementation?
MediaStore has not discovered your newly-created files yet. What you see in Windows — and in many on-device "gallery" apps — is based on what MediaStore has indexed.
Use MediaScannerConnection and its scanFile() method to tell MediaStore about your file, once you have written out your data to disk:
public void scanFile(Context ctxt, File f, String mimeType) {
MediaScannerConnection
.scanFile(ctxt, new String[] {f.getAbsolutePath()},
new String[] {mimeType}, null);
}
or, in Kotlin:
fun scanFile(ctxt: Context, f: File, mimeType: String) {
MediaScannerConnection.scanFile(ctxt, arrayOf(f.getAbsolutePath()), arrayOf(mimeType), null)
}

Get the public/root directory of a secondary external storage device in Android?

In android I am able to get my phone's removable external storage by use of:
for (File f : context.getExternalFilesDirs("/"))
if (Environment.isExternalStorageRemovable(f))
Log.println(Log.DEBUG, "#", f.getAbsolutePath());
However, this returns /storage/8E6A-06FF/Android/data/test.application/files which isn't what I want as I simply want the removable's root path /storage/8E6A-06FF/. How can I can get the root path of my phone's removable storage?
You can try this one it is works perfectly for me.It works like a charm with all Os's version.I didn't found any issue so far with this function.
public static String getSDPath() {
String filepath = "";
String[] strPath = {"/storage/sdcard1", "/storage/extsdcard",
"/storage/sdcard0/external_sdcard", "/mnt/extsdcard",
"/mnt/sdcard/external_sd", "/mnt/external_sd",
"/mnt/media_rw/sdcard1", "/removable/microsd", "/mnt/emmc",
"/storage/external_SD", "/storage/ext_sd",
"/storage/removable/sdcard1", "/data/sdext", "/data/sdext2",
"/data/sdext3", "/data/sdext4", "/emmc", "/sdcard/sd",
"/mnt/sdcard/bpemmctest", "/mnt/sdcard/_ExternalSD",
"/mnt/sdcard-ext", "/mnt/Removable/MicroSD",
"/Removable/MicroSD", "/mnt/external1", "/mnt/extSdCard",
"/mnt/extsd", "/mnt/usb_storage", "/mnt/extSdCard",
"/mnt/UsbDriveA", "/mnt/UsbDriveB"};
for (String value : strPath) {
File f = null;
f = new File(value);
if (f.exists() && f.isDirectory()) {
filepath = value;
break;
}
}
return filepath;
}
Try this:
for (File f : context.getExternalFilesDirs("/"))
if (Environment.isExternalStorageRemovable(f))
Log.println(Log.DEBUG, "#", f.getParentFile().getParentFile().getParentFile().getParent());
context.getExternalFilesDirs() will always returns application-specific directory. But the good thing is that application-specific directories are always 4 level deep from the root folder of the storage device. So calling getParentFile() four times on the File f instead of f.getAbsolutePath() will get you the root path of your phone's removable storage.
Maybe just split it at Android?
I tested it, and it works after I request for permission - WRITE_EXTERNAL_STORAGE.
fun getBaseDir(dir: File): String {
val absPath = dir.absolutePath
return if (absPath.contains("/Android")) {
absPath.split("/Android")[0]
} else {
absPath
}
}
This will loop through files on sdcard root directory. If you want primary storage, just change [1] to [0].
getExternalFilesDirs returns paths to your app directory on primary and secondary storage. After splitting the second path by "Android", the first string will contain path to your secondary storage root. for example in my case it was "/storage/B242-37B2/". Working with minSdkVersion 19+.
String sdCardRoot = ContextCompat.getExternalFilesDirs(getApplicationContext(), null)[1].getAbsolutePath().split("Android")[0];
File f = new File(sdCardRoot);
File[] files = f.listFiles();
for (File inFile : files){
Log.d("Files", inFile.getName());
}
Try this one if it helps you. For more information refer this link.
public static HashSet<String> getExternalMounts() {
final HashSet<String> out = new HashSet<String>();
String reg = "(?i).*vold.*(vfat|ntfs|exfat|fat32|ext3|ext4).*rw.*";
String s = "";
try {
final Process process = new ProcessBuilder().command("mount")
.redirectErrorStream(true).start();
process.waitFor();
final InputStream is = process.getInputStream();
final byte[] buffer = new byte[1024];
while (is.read(buffer) != -1) {
s = s + new String(buffer);
}
is.close();
} catch (final Exception e) {
e.printStackTrace();
}
// parse output
final String[] lines = s.split("\n");
for (String line : lines) {
if (!line.toLowerCase(Locale.US).contains("asec")) {
if (line.matches(reg)) {
String[] parts = line.split(" ");
for (String part : parts) {
if (part.startsWith("/"))
if (!part.toLowerCase(Locale.US).contains("vold"))
out.add(part);
}
}
}
}
return out;
}
Here is another approach for the same. Source from here.
Environment.getExternalStorageState() returns path to internal SD
mount point like "/mnt/sdcard"
But the question is about external SD. How to get a path like "/mnt/sdcard/external_sd" (it may differ from device to device)?
Android has no concept of "external SD", aside from external storage, as described above.
If a device manufacturer has elected to have external storage be on-board flash and also has an SD card, you will need to contact that manufacturer to determine whether or not you can use the SD card (not guaranteed) and what the rules are for using it, such as what path to use for it.

Xamarin Android Sqlite Db browser

I have my first smartphone since one week and try make a App with Xamarin.
I use SQLite with EntityFrameworkCore to store data.
It is work fine, but to debug easier I want use a SQLite browser.
The database file path is 'data/data/{AppName}/Database.db'.
I debug from a physic device by USB, but when I explore the device with Windows Explorer I cannot find the SQLite DB file. The 'data/data' folder is not available. Then I can not use a SQLite browser to see the data.
In this post, the author use a Android emulator and can see 'data/data' folder :
https://blog.xamarin.com/building-android-apps-with-entity-framework/
But I prefer use a real device.
Have you a solution?
A solution from the MikeT, in development store the db file in available folder like this :
public static string DatabasePath
{
get
{
var dbFolder = Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryDownloads).AbsolutePath;
var fileName = "database.db";
var dbFullPath = Path.Combine(dbFolder, fileName);
return dbFullPath;
}
}
In production, copy the db file to a available folder.
One a real device you would, I believe, need to root the device to directly access the data.
However, what you could do is to copy the database file elsewhere e.g. to external storage. In following is the core process that I use:-
try {
FileInputStream fis = new FileInputStream(dbfile);
OutputStream backup = new FileOutputStream(backupfilename);
//byte[] buffer = new byte[32768];
int length;
while((length = fis.read(buffer)) > 0) {
backup.write(buffer, 0, length);
}
backup.flush();
backup.close();
fis.close();
catch (IOException e) {
e.printStackTrace();
confirmaction = false;
}
I use the following to get the pathname for backupfilename:-
File dir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS),subdirectory);
this.directory = dir.getPath();
I then add the actual filename (which is user input).
Note! I do checks to determine that EXTERNAL storage is mounted etc.
To get the database path i use:-
String dbfilename = this.getDatabasePath(
DBConstants.DATABASE_NAME).getPath();
dbfile = new File(dbfilename);
This is of course Java, but I'd assume that it could be converted/adapted to suit. The crux of the answer is to get the database file into a place from which you can access it.
Call the ExtractDb method from your activity
public void ExtractDB()
{
var szSqliteFilename = "databasename.db3";
var szSourcePath = new FileManager().GetLocalFilePath(szSqliteFilename);
var szDatabaseBackupPath = Android.OS.Environment.ExternalStorageDirectory.AbsolutePath + "/databasename_Backup.db3";
if (File.Exists(szSourcePath))
{
File.Copy(szSourcePath, szDatabaseBackupPath, true);
Toast.MakeText(this, "Copied", ToastLength.Short).Show();
}
}
Get path to the android device storage as shown below
public class FileManager
{
public string GetLocalFilePath(string filename)
{
string path = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
return Path.Combine(path, filename);
}
}
you need to add the android.permission.WRITE_EXTERNAL_STORAGE permission in your manifest file.

Inconsistent MediaConnection behaviour, some files don't show in Android File Transfer

I have a directory structure of files in external storage. They don't show up in the Android File Transfer app, so I think it's a media scanner problem.
I'm creating them with a FileOutputStream in a directory based on Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS).
I have the following method, called from an activity, so context is an activity (forget that this blocks the main thread for now!):
public void scan(Context context, File base) {
File[] files = base.listFiles();
if (files == null) {
return;
} else {
for (File file : files) {
if (file.isFile()) {
String path = file.getAbsolutePath();
MediaScannerConnection.scanFile(context, new String[]{path}, null, null);
Log.e("Langstroth", path);
} else if (file.isDirectory()) {
this.scan(context, file);
}
}
}
}
public void scan(Context context) {
this.scan(context, this.baseDir);
}
}
The output of the log is as expected:
E/MyApp﹕ /storage/emulated/0/Documents/Langstroth/sample/5000/1430576404874.wav
E/MyApp﹕ /storage/emulated/0/Documents/Langstroth/sample/5000/1430577209491.wav
And then lots of:
E/MyApp﹕ Scan completed path /storage/emulated/0/Documents/Langstroth/sample/5000/1430576404874.wav uri content://media/external/audio/media/7836
E/MyApp﹕ Scan completed path /storage/emulated/0/Documents/Langstroth/sample/5000/1430577209491.wav uri content://media/external/audio/media/7838
This proves that the files exist. They don't show up in the Android File Transfer though.
Here's the strange thing. Another method:
public void otherDemo(Context context, File baseDir) {
String newPath = baseDir.getAbsolutePath() + "/some/random/dirs";
Log.e("Langstroth", "New path " + newPath);
File dir = new File(newPath);
dir.mkdirs();
Log.e("Langstroth", dir.exists() ? "Dir exists": "Dir does not exist");
File f = new File(dir, "myfile.txt");
try {
new BufferedOutputStream(new FileOutputStream(f)).close();
} catch (IOException e) {
e.printStackTrace();
}
Log.e("Langstroth", f.exists() ? "File exists": "File does not exist");
MediaScannerConnection.scanFile(context, new String[]{f.getAbsolutePath()}, null, null);
}
and the log output:
E/MyApp﹕ New path /storage/emulated/0/Documents/Langstroth/some/random/other/dirs
E/MyApp﹕ Dir exists
E/MyApp﹕ File exists
E/MyApp﹕ File: /storage/emulated/0/Documents/Langstroth/some/random/other/dirs/myfile.txt
E/MyApp﹕ Other scan completed path /storage/emulated/0/Documents/Langstroth/some/random/other/dirs/myfile.txt uri content://media/external/file/7842
One test file shows up, the others don't
Proof:
Where are the other files?
Generally speaking, before you let another process work with a file, you want to ensure all bytes are flushed to disk, via getFD().sync(). In particular, this seems to help with the whole media scanning thing.
the files shows up in a .listFiles(), and .exist(), and the callback for the MediaScanner says that it completed correctly. Surely an extant (if empty) file should show up?
The ways of the media scanner are mysterious. :-) IOW, beats me.
Bear in mind that there are multiple moving parts here: your app, the media scanner, the MTP daemon on Android, and your MTP client. The breakdown could be at any stage. If you unplug and re-plug in the device, and now the files show up in your MTP client, my guess would be that the MTP client is working off of a slightly stale cache.

Saving a bitmap to disk/gallery in Android 4.3

I've been using the way the system saves screenshots to save my bitmaps to the disk and gallery. This works in Android 4.2 and before but not in Android 4.3.
Relevant code :
Uri imageUri = resolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
OutputStream out = resolver.openOutputStream(uri);
Full code here.
In 4.3 (new Nexus 7) however, I get a FileNotFoundException on the second line. I couldn't see any changes in 4.3 relevant to this on the website.
So what is the right way to save an image to the disk and gallery?
Verified :
storage is mounted with this method
imageUri is not null (usually something like "content://media/external/images/media/2034")
manifest has permission android.permission.WRITE_EXTERNAL_STORAGE
This is the way I save bitmaps to the Gallery:
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
Uri contentUri; //instantiate Uri with location of image
mediaScanIntent.setData(contentUri);
context.sendBroadcast(mediaScanIntent);
In your manifest file try with change target sdk to 18.-
<uses-sdk android:minSdkVersion="7"
android:targetSdkVersion="18"/>
It might solve your prob(May not). In 4.3 JELLY_BEAN_MR2, android did couple of changes and android clearly written that Your app might misbehave in a restricted profile environment. so please look at http://developer.android.com/about/versions/android-4.3.html
I have these permission in my Manifest.
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
But I am using a Target SDK version 15. Is there a requirement that you have to use a target SDK 18?
BTW:
Here is a sample code for downloading profile pictures from Facebook:
private class DownloadProfilePicTask extends AsyncTask<Void,String,String> {
Bitmap profilePic;
String fileName;
String id;
String type;
URL img_value;
public DownloadProfilePicTask(String i,String ty)
{
id = i;
if(id==null)
{
//Log.v("Id is null", "Error");
}
//Log.v("Download Profile Pic Task initialized for id:",id);
type = ty;
}
#Override
protected String doInBackground(Void...param) {
String root = Environment.getExternalStorageDirectory().toString();
if(root==null)
{
return null;
}
try{
profilePic = BitmapFactory.decodeStream(img_value.openConnection().getInputStream());
}
catch (IOException e) {
e.printStackTrace();
}
if(profilePic == null)
{
//Log.v("profilePic is null", "Error");
}
//Log.v("Root for saving images",root );
File myDir = new File(root + "/saved_images");
myDir.mkdirs();
fileName = root + "/saved_images/" + id + ".png";
//Log.v("filename is ",fileName);
File file = new File (fileName);
fileName = file.getPath();
if (file.exists ()) file.delete ();
try {
FileOutputStream out = new FileOutputStream(file);
profilePic.compress(Bitmap.CompressFormat.PNG, 90, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
return id;
}
#Override
protected void onPreExecute()
{
try
{
img_value = new URL("http://graph.facebook.com/"+id+"/picture?type=" + type);
}
catch (MalformedURLException e) {
e.printStackTrace();
}
}
#Override
protected void onPostExecute(String result) {
}
}
and then I just call:
new DownloadProfilePicTask(id,type).execute();
to download and automatically save images.
Note: You will have to play with filePath a bit for exact location.
There some changes in the fileSystem on Android 4.3 to start to avoid dev. to directly write in "/sdcard" or "/mnt/sdcard" but use the android ExternalStorage system. (http://source.android.com/devices/tech/storage/index.html)
N.B. : ExternalStorage can be an internal memory :p
For your case, have you tryed to use a method based on getExternalStorage ?
(like this : Find an external SD card location)

Categories

Resources