This is my first Android application I'm making, so I'm a bit lost on what the terms are for everything.
I'm trying to copy a file I have in my /raw/ directory to the root of my SD card.
Currently, my (used Stackoverflow, didn't write this fully myself) code looks like this:
btnWriteSDFile.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
try {
File myFile = new File(Environment.getExternalStorageDirectory().getAbsolutePath(), "fileName.docx");
myFile.createNewFile();
Toast.makeText(v.getContext(),"Wrote line", Toast.LENGTH_SHORT).show();
FileOutputStream fOut = new FileOutputStream(myFile);
OutputStreamWriter myOutWriter =new OutputStreamWriter(fOut);
myOutWriter.append("testFile");
myOutWriter.close();
fOut.close();
Toast.makeText(v.getContext(),"Done writing SD 'mysdfile.txt'", Toast.LENGTH_SHORT).show();
}
catch (Exception e)
{
Toast.makeText(v.getContext(), e.getMessage(),Toast.LENGTH_SHORT).show();
}
}
});
I get the error: "open failed; EACCES (Permission Denied)".
My Manifest looks like this:
<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">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permisson.READ_EXTERNAL_STORAGE" />
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
Clarification to avoid duplication:
Besides the fact that I get this error: how can I write a /raw/file.docx file to my SD card root?
According to this documentation you have to add the uses permission inside and immediate to <manifest> tag
<manifest>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permisson.READ_EXTERNAL_STORAGE" />
...
<application>
...
<activity>
...
</activity>
</application>
</manifest>
like this
<manifest>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permisson.READ_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">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
this should work for copying file
InputStream in = getResources().openRawResource(R.raw.fileNameYourGoingToCopy);
String path = Environment.getExternalStorageDirectory() + "/anyFolder" + File.separator + fileNameWithExtension;
FileOutputStream out = new FileOutputStream(path);
byte[] buff = new byte[1024];
int read = 0;
try {
while ((read = in.read(buff)) > 0) {
out.write(buff, 0, read);
}
} finally {
in.close();
out.close();
}
Related
/**
* Downloading file in background thread
* */
#Override
protected String doInBackground(String... f_url) {
int count;
try {
String root = Environment.getExternalStorageDirectory().toString(); // "/storage/emulated/0"
System.out.println("Downloading");
URL url = new URL(f_url[0]); //http://xxx.168.2.200/tmp/bsp1.txt;
URLConnection conection = url.openConnection();
conection.connect();
// getting file length
int lenghtOfFile = conection.getContentLength();
// input stream to read file - with 8k buffer
InputStream input = new BufferedInputStream(url.openStream(), 8192);
// Output stream to write file
OutputStream output = new FileOutputStream(fileNameDaten);
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
// writing data to file
output.write(data, 0, count);
}
// flushing output
output.flush();
// closing streams
output.close();
input.close();
} catch (Exception e) {
String fel=e.getMessage();
Log.e("Error: ", e.getMessage());
}
return null;
}
/**
* After completing background task
* **/
#Override
protected void onPostExecute(String file_url) {
System.out.println("Downloaded");
}
}
doInBackground is running until "conection.connect();"
After about 3 minutes the app will continue with an exception:
"Failed to connect to /xxx.168.2.200:80"
In principle, I can access the web server's HTML pages with my mobile phone.
My question: What does the error message mean and how can I fix the error?
networkSecurityConfig : not added to manifest, how to formulate ?
app targets android 9, API 28 only partially installed
I will use the app only for local adresses
Follows manifest:enter code here
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="de.wicki.pdftextapplication">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_INTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />
<uses-permission android:name="android.permission.DOWNLOAD_WITHOUT_NOTIFICATION" />
<application
android:usesCleartextTraffic="true"
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
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=".UploadToServer"></activity>
</application>
</manifest>
I have been trying to launch my app after boot completed, but somehow it works on virtual devices but not working on real devices. It is working on from till android 8 but not from android 9 in devices like Xiaomi, Oneplus and Realme. I have read the documents, it was stated that developers can still use receive_boot_completed. I am not able to solve this problem any help will be appreciated.
Manifest File:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.restartexample">
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme"
tools:ignore="GoogleAppIndexingWarning">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name=".RestartReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<action android:name="android.intent.action.QUICKBOOT_POWERON"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</receiver>
</application>
</manifest>
RestartReceiver:
public class RestartReceiver extends BroadcastReceiver {
private static String TAG = RestartReceiver.class.getSimpleName();
#Override
public void onReceive(Context context, Intent intent) {
Log.d("Receiver","onReceive");
StringBuilder sb = new StringBuilder();
sb.append("Action: " + intent.getAction() + "\n");
sb.append("URI: " + intent.toUri(Intent.URI_INTENT_SCHEME).toString() + "\n");
String log = sb.toString();
Log.d(TAG, log);
Toast.makeText(context, log, Toast.LENGTH_LONG).show();
}
}
I am developing an app in API 27, I have a problem in opening a PDF from the asset folder. I have tried lots of different approaches, but still not successful.
When the PDF opens, a blank black screen appears. I checked the file.exist() return false in all steps it returns 'false'.
I really need help about this topic. In addition, I tried both internal and external storage. Also tried different intent tag. But still no luck.
Here is the function I've used.
private void openPDFFiles(String fileName) //fileName is the pdf file name which is keep in assets folder. ex file.pdf
{
AssetManager assetManager = getApplicationContext().getAssets();
InputStream in = null;
OutputStream out = null;
File imagePath = new File(getFilesDir(), "Videos");
File file = new File(imagePath, "1.pdf");
try {
in = assetManager.open(fileName);
out = openFileOutput(file.getName(), MODE_PRIVATE);
copyFile(in, out);
in.close();
in = null;
out.flush();
out.close();
out = null;
} catch (Exception e) {
System.out.println(e);
}
try {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(FileProvider.getUriForFile(getApplicationContext(), "com.administrator.siemens.android.fileprovider", file), "application/pdf");
//intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
//intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
//intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(intent);
} catch (RuntimeException ex) {
Toast.makeText(Document.this, "There's no PDF Reader found in your device", Toast.LENGTH_SHORT).show();
}
private void copyFile(InputStream in, OutputStream out) throws IOException {
byte[] buffer = new byte[1024];
int read;
while ((read = in.read(buffer)) != -1) {
out.write(buffer, 0, read);
}
System.out.println("1222222222");
}
manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.administrator.siemens">
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="#drawable/me"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.administrator.siemens.android.fileprovider"
android:grantUriPermissions="true"
android:exported="false">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="#xml/filepath" />
</provider>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
filepath.xml:
<paths>
<files-path name="my_docs" path="Videos/"/>
</paths>
Really thanks!, if anyone can help me.
I am getting this error I change everything almost setwallpaper to set wallpapermanager and trget sdk and min is also set. Still not find the solution how to remove manifest error.
Permission added already for set_Wallpaper.
[2015-01-17 07:51:51 -
com.android.ide.eclipse.adt.internal.project.AndroidManifestHelper]
Parser exception for
C:\Workspaces\MakingFun\AndroidMe\AndroidManifest.xml: The element
type "uses-permission" must be terminated by the matching end-tag
"". [2015-01-17 07:51:51 - AndroidMe] Error in an
XML file: aborting build.
========================================================================
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.androidme"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.SET_WALLPAPER"/>
<uses-permission />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.androidme.Splash"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.androidme.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.example.androidme.MainActivity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="com.example.androidme.Menu"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.example.androidme.Menu" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="com.example.androidme.TextPlay"
android:label="#string/app_name" >
</activity>
<activity
android:name="com.example.androidme.Email"
android:label="#string/app_name" >
</activity>
<activity
android:name="com.example.androidme.Camera"
android:label="#string/app_name" >
</activity>
</application>
</manifest>
Code :
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.bsetwalpapr:
try {
WallpaperManager Setwalpaper = WallpaperManager.getInstance(this);
Setwalpaper.setBitmap(bmp);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
case R.id.ibtakenimage:
i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(i, cameraData);
break;
}
}
Actually you defined <uses-permission /> but what uses permission? Sure thing you need to add name there or remove this useless tag at all.
Check the line:
<uses-permission android:name="android.permission.SET_WALLPAPER"/>
<uses-permission />
I want to sync a txt file in android internal storage (or sdcard for that matter) and have it automatically sync to a txt in my dropbox. i've used dropbox api but don't have hold on on how to achieve this. help needed please.
Update:
Here's my function to get the txt content to LogCat for now:
public void getFromDbx(){
try {
mDbxAcctMgr.startLink((Activity)this, REQUEST_LINK_TO_DBX);
final String TEST_DATA = " Database does not exist yet";
final String TEST_FILE_NAME = "data.txt";
DbxPath testPath = new DbxPath(DbxPath.ROOT, TEST_FILE_NAME);
// Create DbxFileSystem for synchronized file access.
DbxFileSystem dbxFs = DbxFileSystem.forAccount(mDbxAcctMgr.getLinkedAccount());
// Print the contents of the root folder. This will block until we can
// sync metadata the first time.
//dont need this code -- ahmed
// Create a test file only if it doesn't already exist.
if (!dbxFs.exists(testPath)) {
DbxFile testFile = dbxFs.create(testPath);
try {
testFile.writeString(TEST_DATA);
} finally {
testFile.close();
}
}
// Read and print the contents of test file. Since we're not making
// any attempt to wait for the latest version, this may print an
// older cached version. Use getSyncStatus() and/or a listener to
// check for a new version.
if (dbxFs.isFile(testPath)) {
String resultData;
DbxFile testFile = dbxFs.open(testPath);
try {
resultData = testFile.readString();
} finally {
testFile.close();
}
Log.i("dbx",resultData);
} else if (dbxFs.isFolder(testPath)) {
}
} catch (IOException e) {
}
My OnCreate:
super.onCreate(savedInstanceState);
setContentView(R.layout.main_screen);
//connect dbx to account
mDbxAcctMgr = DbxAccountManager.getInstance(getApplicationContext(), appKey, appSecret);
//get data from ems_data.txt from dropbox account
getFromDbx();
My OnActivityResult:
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_LINK_TO_DBX) {
if (resultCode == Activity.RESULT_OK) {
getFromDbx();
} else {
Log.i("dbxems", "Link to Dropbox failed or was cancelled.");
}
} else {
super.onActivityResult(requestCode, resultCode, data);
}
}
My Manifest File:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.testdatabaseactivity"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.CALL_PHONE" >
</uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:debuggable="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.testdatabaseactivity.TestDatabaseActivity"
android:label="#string/app_name" >
<intent-filter>
</intent-filter>
</activity>
<instrumentation
android:name="android.test.InstrumentationTestRunner"
android:label="your tests label"
android:targetPackage="com.example.testdatabaseactivity" />
<uses-library android:name="android.test.runner" />
<activity
android:name="com.example.testdatabaseactivity.MainScreenActivity"
android:label="#string/title_activity_main_screen" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.testdatabaseactivity.SearchByName"
android:label="#string/title_activity_search_by_name"
android:windowSoftInputMode="stateHidden" >
</activity>
<activity android:name="com.dropbox.sync.android.DbxAuthActivity" />
<activity
android:name="com.dropbox.client2.android.AuthActivity"
android:launchMode="singleTask" >
<intent-filter>
<data android:scheme="db-KEY_HERE" />
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.BROWSABLE" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<service
android:name="com.dropbox.sync.android.DbxSyncService"
android:enabled="true"
android:exported="false"
android:label="Dropbox Sync" />
</application>
</manifest>
EDIT: there is no error, the app shows the auth screen, I click allow and then nothing happens. I can press allow 10 times and nothing happens at all..
In getFromDbx, you call startLink, which brings up the UI to link an account. When that succeeds, your onActivityResult is executed, where you call getFromDbx, which calls startLink again. :-) Looks like an infinite loop to me.