BroadcastReceiver on BOOT_COMPLETED not getting called - android

I wanted to execute a piece of code as soon as every time Device is booted up.
Taking help from the answer provided here.
I have created an application which only contains one class which extends BroadcastReceiver. (There is no other activity/service as on now)
Currently ony for testing purpose, my BroadcastReceiver should create a file 'Log.txt' on SD card (if not exits) and append the file with current time stamp.
But right now I can not see any file getting created.
Please suggest where am I wrong.
Code:
public class ServiceStarter extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (isExternalStorageWritable() && isExternalStorageReadable()) {
String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/saved_text"); // After boot this saved_text is not getting created
if (!myDir.exists()) {
myDir.mkdirs();
}
String fname = "Log.txt";
File file = new File(myDir, fname);
try {
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(file, true)));
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(Calendar.getInstance().getTime());
out.println("logged :: " + timeStamp);
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
System.out.println("Done");
}
/* Checks if external storage is available to 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;
}
}
Manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android.test"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="15"
android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" >
</uses-permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" >
</uses-permission>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" >
</uses-permission>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<!-- Start the Service if applicable on boot -->
<receiver
android:name="com.android.test.ServiceStarter"
android:enabled="true"
android:exported="false" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>
</manifest>

Related

Android emulator open failed: ENOENT (No such file or directory)

I need help with permission to save file in Android emulator ... I've had added
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-feature android:name="android.hardware.camera" android:required="true" />
in AndroidManifest.xml
My code for save file:
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
Bundle extras = data.getExtras();
Bitmap imageBitmap = (Bitmap) extras.get("data");
imageView.setImageBitmap(imageBitmap);
}
String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/Img");
myDir.mkdirs();
long n = System.currentTimeMillis();
String fname = "IMG_" + n + ".jpeg";
file = new File(myDir, fname);
if (file.exists())
file.delete();
try {
FileOutputStream out2 = new FileOutputStream(file);// here fire exception
imageBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out2);
out2.flush();
out2.close();
} catch (Exception e) {
e.printStackTrace();
}
}
AndroidManifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.denis.calculator">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".InfoActivity"></activity><!-- ATTENTION: This was auto-generated to add Google Play services to your project for
App Indexing. See https://g.co/AppIndexing/AndroidStudio for more information. -->
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
</application>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-feature android:name="android.hardware.camera" android:required="true" />
Any ideas pls?
Solution is downgrade from Nougat to KitKat! thanks for advices
Replace the below lines
String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/Img");
With
File myDir = new File(Environment.getExternalStorageDirectory(),"Img");
Also remove the below lines from your code
if (file.exists())
file.delete();
Because the way you defining the file name will never gonna have the same file name. So validating the existence of file is irrelevant here.
You also have to ask writing permission with the user if your app is going to be used for Android 6.0 or above. Refer the link given below for details:
https://developer.android.com/training/permissions/requesting.html
You can use the solution defined in the library I've recently created this repository including a demo for Permission.
https://github.com/eeshan-jamal/DroidLibX
I will later make it available through Maven but for now you have to import the droidlibx library module in your project.

Unable to create external directory

I am unable to create a directory in the external storage folder. I have created a new project, modified the manifest and added the code as seen below. Any help would be greatly appreciated. I updated my post to include that the code provided below does not work. The only thing I can think is the emulator is the problem. I am running Nexis API 32 x64.
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest package="com.us.foobar"
xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>
Source:
public void mkdir() throws Exception
{
String root = Environment.getExternalStorageDirectory().getAbsolutePath();
String dir = root + "/Foobar";
File file = new File(dir);
if (!file.exists())
{
if (!file.mkdirs())
{
throw new Exception("Unable to create application data directory.\r\n\r\nDirectory: " + dir);
}
}
}
I call the same function on onCreate and works fine. Look at my code:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
test();
}
public void test() {
String root = Environment.getExternalStorageDirectory().getAbsolutePath();
String dir = root + "/Foobar";
File file = new File(dir);
if (!file.exists()) {
if (!file.mkdirs()) {
Log.e("Error", "Unable to create application data directory.\r\n\r\nDirectory: " + dir);
}
}
}
}
Using API 23 I had to request the permissions prior to writing to the file system as follows:
ActivityCompat.requestPermissions(
this,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
REQUEST_EXTERNAL_STORAGE
);

Android FileNotFoundException in uploading Photo

I have problem with finding photo after saving.
Here is how i am creating Intent to take photo from camera and save filePath to it.
private void takePhoto(int position)
{
Meter meter = adapter.getItem(position);
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
File tempFile = createTempFile(meter.id, JPEG_FILE_SUFFIX, storageDir);
if(tempFile != null)
{
lastPhotoPath = tempFile.getAbsolutePath();
Log.d(TAG, "temp picture path=" + lastPhotoPath);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(tempFile));
try
{
startActivityForResult(takePictureIntent, TAKE_PHOTO_REQUEST);
lastPhotoPosition = position;
}
catch(ActivityNotFoundException exc)
{
Log.e(TAG, "activity to take photo not found");
}
}
}
Then later i want to upload this image to server.
Here is how i am doing this.
public void compose(OutputStream out) throws DataStorageException
{
Log.d("MainMenuActivity", "file not found in path " + path);
InputStream in = null;
try
{
in = new BufferedInputStream(new FileInputStream(path));
// TODO: there is a better way
byte[] buf = new byte[8192];
while(true)
{
int length = in.read(buf);
if(length < 0)
break;
out.write(buf, 0, length);
}
}
catch(FileNotFoundException exc)
{
throw new DataStorageInternalErrorException("FileNotFoundException, FileRequestComposer compose");
}
catch(IOException exc)
{
// TODO: probably network error
throw new DataStorageInternalErrorException("IOException, FileRequestComposer compose");
}
finally
{
if(in != null)
{
try
{
in.close();
}
catch(IOException exc)
{
// FIXME
}
}
}
}
I check filePath from saving and filePath to save they both completely identical:
temp picture path=/mnt/sdcard/Pictures/19520983731349.jpg
file not found in path /mnt/sdcard/Pictures/19520983731349.jpg
And the exception is thrown in method compose()
FileNotFoundException, FileRequestComposer compose
Any ideas where i am doing wrong?
P.S. And i can't see this file in /mnt/sdcard/Pictures mb. he is lost or somethinf like this? Please suggest any ideas.
P.P.S Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.vodomer"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="7"
android:targetSdkVersion="19"/>
<uses-feature android:name="android.hardware.camera" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<application android:label="#string/app_name"
android:name="com.vodomer.DatabaseApplication" >
<activity android:name="Vodomer"
android:label="#string/app_name"
android:icon="#drawable/icon"
android:theme="#android:style/Theme.NoTitleBar"
android:screenOrientation="portrait"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".activity.AddressesListActivity"
android:theme="#android:style/Theme.NoTitleBar"
android:screenOrientation="portrait"
>
</activity>
<activity android:name=".activity.MainMenuActivity"
android:theme="#android:style/Theme.NoTitleBar"
android:screenOrientation="portrait"
>
</activity>
<activity android:name=".activity.MetersActivity"
android:theme="#android:style/Theme.NoTitleBar"
android:windowSoftInputMode="adjustPan"
android:screenOrientation="portrait"
>
</activity>
<activity android:name=".activity.PersonalAccountActivity"
android:theme="#android:style/Theme.NoTitleBar"
android:screenOrientation="portrait"
>
</activity>
</application>
</manifest>
The problem is likely with the way you create File. Instead of createTempFile try regular constructor :
File tempFile = new File(storageDir, meter.id + JPEG_FILE_SUFFIX);
Generally, you also want to make sure that your storageDir folder exists and create it with mkdirs if needed.
Add this path for storing image on sdcard.
final String uploadFilePath = "/mnt/sdcard/";

Directory does and doesn't exist

I am trying to write a file to the SD card on my android phone, but it seems to be having issues with the directory. It exists but doesnt exist. By this I mean when I go to the file browser on the android phone, and look for the GPStracker folder I cannot find it. Yet in the program it seems to acknowledge that this directory exists when it checks the dir.exists() if condition below. Then when it gets to the canWrite() function it always returns false. I have no idea what is wrong at this point, anyone have any debugging ideas? Thanks.
boolean mExternalStorageAvailable = false;
boolean mExternalStorageWriteable = false;
String state = Environment.getExternalStorageState();
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;
}
^ These both return true now, got that issue fixed.
Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.tinywebteam.gpstracker"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.tinywebteam.gpstracker.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Code:
try {
File sdCard = Environment.getExternalStorageDirectory();
File dir = new File(sdCard + "/GPStracker/data");
if (!dir.exists()) {
if (!dir.mkdirs())
throw new FileNotFoundException("Couldn't make directory.");
if (!dir.isDirectory())
throw new FileNotFoundException("Couldn't verify directory.");
}
File fileOut = new File(dir, "GPSTracker_" + ts + ".csv");
if (fileOut.canWrite()) {
FileOutputStream out = new FileOutputStream(fileOut);
out.write(fileStr.getBytes());
out.close();
System.out.println("File written successfully");
} else {
System.out.println("Could not write to file");
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

Trouble Creating a Directory on the SD Card

I am trying to have my app create a directory on the root of the SD Card, if that directory does not already exist. When I run the app, the "Creating Home Directory..." toast notification displays, but the directory is not created... What am i doing wrong?? (P.S. permissions are set for writing to external storage)
public class Main extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Check SD Card for Read/Write
boolean mExternalStorageWriteable = false;
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
// We can read and write the media
mExternalStorageWriteable = true;
} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
// We can only read the media
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
mExternalStorageWriteable = false;
}
if (mExternalStorageWriteable == false) {
//Toast Notification that SD Card is not Accessible
Context context = getApplicationContext();
CharSequence text = "SD Card is NOT Accessable, Please Remount and Try Again";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
} else {
//Check If Home Dir Exists
boolean exists = (new File("/AndGuard/")).exists();
if (exists) {
Context context = getApplicationContext();
CharSequence text = "Accessing Data Files...";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
} else {
Context context = getApplicationContext();
CharSequence text = "Creating Home Directory...";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
// Create a directory; all non-existent ancestor directories are
// automatically created
// Create a directory; all non-existent ancestor directories are
// automatically created
File root = Environment.getExternalStorageDirectory();
boolean success = (new File(root,"directoryName")).mkdirs();
if (!success) {
// Directory creation failed
}
}
}
Here is my Manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="soapbox.sym3try.andguard"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".Main"
android:label="#string/app_name">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-sdk android:minSdkVersion="4" />
</manifest>
I tried this as well, it didn't work, but it might be a step in the right direction as it creates the directory and populates it with a file from res/raw all in one step:
public boolean saveas(int ressound){
byte[] buffer=null;
InputStream fIn = getBaseContext().getResources().openRawResource(ressound);
int size=0;
try {
size = fIn.available();
buffer = new byte[size];
fIn.read(buffer);
fIn.close();
} catch (IOException e) {
// TODO Auto-generated catch block
return false;
}
String path="/sdcard/AndGuard/List1";
String filename="hosts";
boolean exists = (new File(path)).exists();
if (!exists){new File(path).mkdirs();}
FileOutputStream save;
try {
save = new FileOutputStream(path+filename);
save.write(buffer);
save.flush();
save.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
return false;
} catch (IOException e) {
// TODO Auto-generated catch block
return false;
}
return exists;
}
The uses-permission should be in the manifest xml tag I think, check out http://developer.android.com/guide/topics/manifest/uses-permission-element.html
This may not do anything, but right after WRITE_EXTERNAL_STORAGE" in your manifest add this permission
android:name="android.permission.READ_PHONE_STATE"
The uses-permission should be in manifest tag, not in activity.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="soapbox.sym3try.andguard"
android:versionCode="1"
android:versionName="1.0">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
<uses-sdk android:minSdkVersion="4" />
<application
android:icon="#drawable/icon"
android:label="#string/app_name">
<activity
android:name=".Main"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
You can't create any files in the root directory, so I assume it must me
File root = Environment.getExternalStorageDirectory();
boolean exists = (new File(root, "AndGuard")).exists();
Hardcoding /sdcard/ is wrong.
String path="/sdcard/AndGuard/List1"
right
File path = new File(root, "AndGuard/List1");
And
save = new FileOutputStream(path+filename);
will give you
"/sdcard/AndGuard/List1hosts"
So it must be
save = new FileOutputStream(new File(path, filename));
//create file dir
File wallpaperDirectory = new File("/sdcard/example");
// have the object build the directory structure, if needed.
wallpaperDirectory.mkdirs();
This works for me. You will need to set
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE">

Categories

Resources