I'm currently developing a live wallpaper that reads from external storage.
When the device is booting up, I assume it's possible for the live wallpaper to be launched before the storage is ready. Especially if its doing the periodic error check. Others are reporting issues and I think this is the reason. I can't seem to test this, because the external storage seems to mount instantly on my device, and I'm not sure how to force it to do the error check. So my first question is, does the system actually way for the BOOT_COMPLETED intent before it launches the live wallpaper.
If not, what is the proper way to wait for the external storage to be ready. I'm thinking of calling something like this in the beginning of the app
public void waitForExternalStorage()
{
while(Environment.getExternalStorageState().equals(Environment.MEDIA_CHECKING))
{
try { Thread.sleep(1000L); }
catch(InterruptedException e) { e.printStackTrace(); }
}
}
Do I have to check for other cases, in case it goes MEDIA_REMOVED -> MEDIA_UNMOUNTED -> MEDIA_CHECKING(optional) -> MEDIA_READY on boot?
You can register a BroadcastReceiver to listen to changes in the external storage state:
BroadcastReceiver externalStorageStateReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
updateExternalStorageState();
}
};
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_MEDIA_MOUNTED);
filter.addAction(Intent.ACTION_MEDIA_REMOVED);
registerReceiver(mExternalStorageReceiver, filter);
updateExternalStorageState(); // You can initialize the state here, before any change happens
And in updateExternalStorageState() you can check the actual state after the change:
protected void updateExternalStorageState() {
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
// SD card is mounted and ready for use
} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
// SD card is mounted, but it is read only
} else {
// SD card is unavailable
}
}
use broadcast receiver, listen for Intent.ACTION_MEDIA_MOUNTED : Broadcast Action: External media is present and mounted at its mount point.
I needed exactly that. I am reading a configuration file from the SD card and my on-boot running app simply cannot run without reading it. A simple solution is to wait a maximum of 15-30 seconds for the SD card to be automatically mounted by the Android OS. If not, throw an exception. Here's the code. Just increase the max count limit to increase the wait time. The ExternalStorageNotReadyException is a custom exception.
public void awaitExternalStorageInitialization()
throws ExternalStorageNotReadyException {
boolean mExternalStorageAvailable = false;
boolean mExternalStorageWriteable = false;
int count = 0;
do {
String state = Environment.getExternalStorageState();
if(count > 0) {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
Log.e(Constants.LOG_TAG, e.getMessage(), e);
}
}
if (Environment.MEDIA_MOUNTED.equals(state)) {
// We can read and write the media
mExternalStorageAvailable = mExternalStorageWriteable = true;
} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
// We can only read the media
mExternalStorageAvailable = true;
mExternalStorageWriteable = false;
} else {
// Something else is wrong. It may be one of many other states,
// but all we need
// to know is we can neither read nor write
mExternalStorageAvailable = mExternalStorageWriteable = false;
}
count++;
} while ((!mExternalStorageAvailable) && (!mExternalStorageWriteable) && (count < 5));
if(!mExternalStorageWriteable)
throw new ExternalStorageNotReadyException("External storage device (SD Card) not yet ready");
}
Related
Please help with clearing cache in android. I am able to delete cache with below code on android version 6.0. When i am trying it in 7.1 nothing changes. what am i missing?
//method to delete cache- issue reported
public static void deleteCache(Context context) {
try {
File dir = context.getCacheDir();
deleteDir(dir);
Log.d("cache clear", "cache delete "+dir);
} catch (Exception e) { e.printStackTrace();}
}
public static boolean deleteDir(File dir) {
if (dir != null && dir.isDirectory()) {
Log.d("cache clear", "cache delete 1");
String[] children = dir.list();
for (int i = 0; i < children.length; i++) {
boolean success = deleteDir(new File(dir, children[i]));
if (!success) {
return false;
}
}
return dir.delete();
} else if(dir!= null && dir.isFile()) {
return dir.delete();
} else {
return false;
}
}
From Android API 19 you can do that:
((ActivityManager)context.getSystemService(ACTIVITY_SERVICE)).clearApplicationUserData();
Official docs:
clearApplicationUserData
added in API level 19
public boolean clearApplicationUserData ()
Permits an application to erase its own data from disk. This is equivalent to the user choosing to clear the app's data from within the device settings UI. It erases all dynamic data associated with the app -- its private data and data in its private area on external storage -- but does not remove the installed application itself, nor any OBB files. It also revokes all runtime permissions that the app has acquired, clears all notifications and removes all Uri grants related to this application.
Returns
boolean true if the application successfully requested that the application's data be erased; false otherwise.
EDIT
From your Activity don't call
deleteCache(this)
but call
deleteCache(getApplicationContext())
I have this code, that should be executed, when SD card is removed from an Android device (by remove, I understand, that user selects remove from Android device menu or configuration, just as you remove USB device or SD card from any other system, before phisically removing the card):
public void kartkontrol()
{
String state = android.os.Environment.getExternalStorageState();
if (state.equals(android.os.Environment.MEDIA_MOUNTED))
{
// We can read and write the media
Durum=" Hafıza Kartı Var (Okuma/Yazma)";
}
else if (android.os.Environment.MEDIA_MOUNTED_READ_ONLY.equals(state))
{
// We can only read the media
Durum=" Hafıza Kartı Var (Okuma İzni)";
}
else
{
// No external media
Durum=" Hafıza Kartı Yok";
}
}
Card should be uninstalled and removed, but it seems, that it is actually installed during this process.
What am I doing wrong?
You can find further information in the Android documentation.
There is a nice post here talking about detecting mounted sd card via USB.
you can also use this useful method instead :
static public boolean hasStorage(boolean requireWriteAccess) {
//TODO: After fix the bug, add "if (VERBOSE)" before logging errors.
String state = Environment.getExternalStorageState();
Log.v(TAG, "storage state is " + state);
if (Environment.MEDIA_MOUNTED.equals(state)) {
if (requireWriteAccess) {
boolean writable = checkFsWritable();
Log.v(TAG, "storage writable is " + writable);
return writable;
} else {
return true;
}
} else if (!requireWriteAccess && Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
return true;
}
return false;
}
code when I remove the card is mounted, but it must be unmounted. I solved this problem with another code. running smoothly. no problem
sSDpath = null;
fileCur = null;
for (String sPathCur : Arrays.asList("ext_card", "external_sd", "ext_sd", "external","extSdCard", "externalSdCard")) // external sdcard
{
fileCur = new File("/mnt/", sPathCur);
if (fileCur.isDirectory() && fileCur.canWrite()) {
sSDpath = fileCur.getAbsolutePath();
kartk.setText("Hafıza Kartı Takılı");
Toast.makeText(getApplicationContext(), "kkkBirinci", Toast.LENGTH_LONG).show();
break;
}
}
fileCur = null;
if (sSDpath == null) {
sSDpath = Environment.getExternalStorageDirectory().getAbsolutePath();
kartk.setText("Hafıza Kartı Takılı Değil");
Toast.makeText(getApplicationContext(), "kkkikinci", Toast.LENGTH_LONG).show();
}
I am currently writing an android app that logs the accelerometer. (its a test app at the moment so i can prototype an algorithm.
To write out a list of SensorEventStore's (which is just a way of storing the data from a SensorEvent) to the SD card from a 30 minute recording, locks up the GUI for about 20 - 30 seconds while writing the file.
I am using the following code to write out the file to the SD card.
#Override
public void onMessage(Messages message, Object param[]) {
if(message == IDigest.Messages.SaveData) {
File folder = (File) param[0];
File accFileAll = new File(folder, startTime + "_all.acc");
FileWriter accFileWriterAll;
try {
accFileWriterAll = new FileWriter(accFileAll);
} catch (IOException e) {
accFileWriterAll = null;
}
for(Iterator<SensorEventStore> i=eventList.iterator(); i.hasNext();) {
SensorEventStore e = i.next();
if(accFileWriterAll != null) {
try {
accFileWriterAll.write(
String.format(
"%d,%d,%f,%f,%f\r\n",
e.timestamp,
e.accuracy,
e.values[0],
e.values[1],
e.values[2]
)
);
accFileWriterAll.flush();
} catch (IOException ex) {
}
}
}
new SingleMediaScanner(RunBuddyApplication.Context, accFileAll);
}
}
Can anyone give me any pointers to make this not lock up the UI, or not have to take the amount of time it currently takes to write out the file.
Firstly you should try to do this in the background. The AsyncTask is fairly well suited for the task.
Other than that, you should remove the flush() statement, and probperly close() your file writer. The flush causes the data to be written to disk in rather small portions, which is really slow. If you leave the filewriter to its own flushing, it will determine a buffer size on its own. When you properly close the FileWriter, the remaining data should be written to disk as well.
Also, you could take a look at "Try with resources" for your filewriter, but that is optional.
I'm working on an Android application that needs to look at what images a user has stored. The problem is that if the user has the sdcard mounted via the USB cable, I can't read the list of images on the disk.
Does anyone know of a way to tell if the usb is mounted so that I could just pop up a message informing the user that it won't work?
If you're trying to access images on the device, the best method is to use the MediaStore content provider. Accessing it as a content provider will allow you to query the images that are present, and map content:// URLs to filepaths on the device where appropriate.
If you still need to access the SD card, the Camera application includes an ImageUtils class that checks if the SD card is mounted as follows:
static public boolean hasStorage(boolean requireWriteAccess) {
//TODO: After fix the bug, add "if (VERBOSE)" before logging errors.
String state = Environment.getExternalStorageState();
Log.v(TAG, "storage state is " + state);
if (Environment.MEDIA_MOUNTED.equals(state)) {
if (requireWriteAccess) {
boolean writable = checkFsWritable();
Log.v(TAG, "storage writable is " + writable);
return writable;
} else {
return true;
}
} else if (!requireWriteAccess && Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
return true;
}
return false;
}
Here is the checkFsWritable missing function in jargonjustin post
private static boolean checkFsWritable() {
// Create a temporary file to see whether a volume is really writeable.
// It's important not to put it in the root directory which may have a
// limit on the number of files.
String directoryName = Environment.getExternalStorageDirectory().toString() + "/DCIM";
File directory = new File(directoryName);
if (!directory.isDirectory()) {
if (!directory.mkdirs()) {
return false;
}
}
return directory.canWrite();
}
public static boolean isSdPresent() {
return android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
}
I apologise for posting a non-Android way of doing this, hopefully someone can provide an answer using the Android API.
You could list the files on the root of the sdcard. If there is none, the sdcard is either entirely blank (unusual, but possible) or it is unmounted. If you try to create an empty file on the sdcard and it fails, it means that you were trying to create a file in the mount-point of the sdcard which would be denied due to a permissions issue so you would know the sdcard was not mounted.
Yes, I know this is ugly....
On jargonjustin's post:
File ImageManager.java
Method hasStorage -->
public static boolean hasStorage(boolean requireWriteAccess) {
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
if (requireWriteAccess) {
boolean writable = checkFsWritable();
return writable;
} else {
return true;
}
} else if (!requireWriteAccess
&& Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
return true;
}
return false;
}
Method checkFsWritable -->
private static boolean checkFsWritable() {
// Create a temporary file to see whether a volume is really writeable.
// It's important not to put it in the root directory which may have a
// limit on the number of files.
String directoryName =
Environment.getExternalStorageDirectory().toString() + "/DCIM";
File directory = new File(directoryName);
if (!directory.isDirectory()) {
if (!directory.mkdirs()) {
return false;
}
}
File f = new File(directoryName, ".probe");
try {
// Remove stale file if any
if (f.exists()) {
f.delete();
}
if (!f.createNewFile()) {
return false;
}
f.delete();
return true;
} catch (IOException ex) {
return false;
}
}
I was using cursor for retrieving the images from sd card and when no sd card was inserted in device the cursor was null.
Actually this is the case when the sdcard volume was unmounted by physically removing card from device. This is the code I've used:
Cursor mCursor = this.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, null, null, null, null);
if (mCursor == null || !mCursor .moveToFirst()) {
/**
*mCursor == null:
* - query failed; the app don't have access to sdCard; example: no sdCard
*
*!mCursor.moveToFirst():
* - there is no media on the device
*/
} else {
// process the images...
mCursor.close();
}
More info: http://developer.android.com/guide/topics/media/mediaplayer.html#viacontentresolver
Before you do any work with the external storage, you should always call getExternalStorageState() to check whether the media is available. The media might be mounted to a computer, missing, read-only, or in some other state. For example, here are a couple methods you can use to check the availability:
/* Checks if external storage is available for read and write */
public boolean isExternalStorageWritable() {
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
return true;
}
return false;
}
/* Checks if external storage is available to at least read */
public boolean isExternalStorageReadable() {
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state) ||
Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
return true;
}
return false;
}
source:
http://developer.android.com/guide/topics/data/data-storage.html
Cool....Check it out...
try {
File mountFile = new File("/proc/mounts");
usbFoundCount=0;
sdcardFoundCount=0;
if(mountFile.exists())
{
Scanner usbscanner = new Scanner(mountFile);
while (usbscanner.hasNext()) {
String line = usbscanner.nextLine();
if (line.startsWith("/dev/fuse /storage/usbcard1")) {
usbFoundCount=1;
Log.i("-----USB--------","USB Connected and properly mounted---/dev/fuse /storage/usbcard1" );
}
}
}
if(mountFile.exists()){
Scanner sdcardscanner = new Scanner(mountFile);
while (sdcardscanner.hasNext()) {
String line = sdcardscanner.nextLine();
if (line.startsWith("/dev/fuse /storage/sdcard1")) {
sdcardFoundCount=1;
Log.i("-----USB--------","USB Connected and properly mounted---/dev/fuse /storage/sdcard1" );
}
}
}
if(usbFoundCount==1)
{
Toast.makeText(context,"USB Connected and properly mounted", 7000).show();
Log.i("-----USB--------","USB Connected and properly mounted" );
}
else
{
Toast.makeText(context,"USB not found!!!!", 7000).show();
Log.i("-----USB--------","USB not found!!!!" );
}
if(sdcardFoundCount==1)
{
Toast.makeText(context,"SDCard Connected and properly mounted", 7000).show();
Log.i("-----SDCard--------","SDCard Connected and properly mounted" );
}
else
{
Toast.makeText(context,"SDCard not found!!!!", 7000).show();
Log.i("-----SDCard--------","SDCard not found!!!!" );
}
}catch (Exception e) {
e.printStackTrace();
}
I'm working on an Android application that needs to look at what images a user has stored. The problem is that if the user has the sdcard mounted via the USB cable, I can't read the list of images on the disk.
Does anyone know of a way to tell if the usb is mounted so that I could just pop up a message informing the user that it won't work?
If you're trying to access images on the device, the best method is to use the MediaStore content provider. Accessing it as a content provider will allow you to query the images that are present, and map content:// URLs to filepaths on the device where appropriate.
If you still need to access the SD card, the Camera application includes an ImageUtils class that checks if the SD card is mounted as follows:
static public boolean hasStorage(boolean requireWriteAccess) {
//TODO: After fix the bug, add "if (VERBOSE)" before logging errors.
String state = Environment.getExternalStorageState();
Log.v(TAG, "storage state is " + state);
if (Environment.MEDIA_MOUNTED.equals(state)) {
if (requireWriteAccess) {
boolean writable = checkFsWritable();
Log.v(TAG, "storage writable is " + writable);
return writable;
} else {
return true;
}
} else if (!requireWriteAccess && Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
return true;
}
return false;
}
Here is the checkFsWritable missing function in jargonjustin post
private static boolean checkFsWritable() {
// Create a temporary file to see whether a volume is really writeable.
// It's important not to put it in the root directory which may have a
// limit on the number of files.
String directoryName = Environment.getExternalStorageDirectory().toString() + "/DCIM";
File directory = new File(directoryName);
if (!directory.isDirectory()) {
if (!directory.mkdirs()) {
return false;
}
}
return directory.canWrite();
}
public static boolean isSdPresent() {
return android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
}
I apologise for posting a non-Android way of doing this, hopefully someone can provide an answer using the Android API.
You could list the files on the root of the sdcard. If there is none, the sdcard is either entirely blank (unusual, but possible) or it is unmounted. If you try to create an empty file on the sdcard and it fails, it means that you were trying to create a file in the mount-point of the sdcard which would be denied due to a permissions issue so you would know the sdcard was not mounted.
Yes, I know this is ugly....
On jargonjustin's post:
File ImageManager.java
Method hasStorage -->
public static boolean hasStorage(boolean requireWriteAccess) {
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
if (requireWriteAccess) {
boolean writable = checkFsWritable();
return writable;
} else {
return true;
}
} else if (!requireWriteAccess
&& Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
return true;
}
return false;
}
Method checkFsWritable -->
private static boolean checkFsWritable() {
// Create a temporary file to see whether a volume is really writeable.
// It's important not to put it in the root directory which may have a
// limit on the number of files.
String directoryName =
Environment.getExternalStorageDirectory().toString() + "/DCIM";
File directory = new File(directoryName);
if (!directory.isDirectory()) {
if (!directory.mkdirs()) {
return false;
}
}
File f = new File(directoryName, ".probe");
try {
// Remove stale file if any
if (f.exists()) {
f.delete();
}
if (!f.createNewFile()) {
return false;
}
f.delete();
return true;
} catch (IOException ex) {
return false;
}
}
I was using cursor for retrieving the images from sd card and when no sd card was inserted in device the cursor was null.
Actually this is the case when the sdcard volume was unmounted by physically removing card from device. This is the code I've used:
Cursor mCursor = this.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, null, null, null, null);
if (mCursor == null || !mCursor .moveToFirst()) {
/**
*mCursor == null:
* - query failed; the app don't have access to sdCard; example: no sdCard
*
*!mCursor.moveToFirst():
* - there is no media on the device
*/
} else {
// process the images...
mCursor.close();
}
More info: http://developer.android.com/guide/topics/media/mediaplayer.html#viacontentresolver
Before you do any work with the external storage, you should always call getExternalStorageState() to check whether the media is available. The media might be mounted to a computer, missing, read-only, or in some other state. For example, here are a couple methods you can use to check the availability:
/* Checks if external storage is available for read and write */
public boolean isExternalStorageWritable() {
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
return true;
}
return false;
}
/* Checks if external storage is available to at least read */
public boolean isExternalStorageReadable() {
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state) ||
Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
return true;
}
return false;
}
source:
http://developer.android.com/guide/topics/data/data-storage.html
Cool....Check it out...
try {
File mountFile = new File("/proc/mounts");
usbFoundCount=0;
sdcardFoundCount=0;
if(mountFile.exists())
{
Scanner usbscanner = new Scanner(mountFile);
while (usbscanner.hasNext()) {
String line = usbscanner.nextLine();
if (line.startsWith("/dev/fuse /storage/usbcard1")) {
usbFoundCount=1;
Log.i("-----USB--------","USB Connected and properly mounted---/dev/fuse /storage/usbcard1" );
}
}
}
if(mountFile.exists()){
Scanner sdcardscanner = new Scanner(mountFile);
while (sdcardscanner.hasNext()) {
String line = sdcardscanner.nextLine();
if (line.startsWith("/dev/fuse /storage/sdcard1")) {
sdcardFoundCount=1;
Log.i("-----USB--------","USB Connected and properly mounted---/dev/fuse /storage/sdcard1" );
}
}
}
if(usbFoundCount==1)
{
Toast.makeText(context,"USB Connected and properly mounted", 7000).show();
Log.i("-----USB--------","USB Connected and properly mounted" );
}
else
{
Toast.makeText(context,"USB not found!!!!", 7000).show();
Log.i("-----USB--------","USB not found!!!!" );
}
if(sdcardFoundCount==1)
{
Toast.makeText(context,"SDCard Connected and properly mounted", 7000).show();
Log.i("-----SDCard--------","SDCard Connected and properly mounted" );
}
else
{
Toast.makeText(context,"SDCard not found!!!!", 7000).show();
Log.i("-----SDCard--------","SDCard not found!!!!" );
}
}catch (Exception e) {
e.printStackTrace();
}