I'm trying to load a Database file from my SD card,but is giving exception.
Below is the exception
org.sqlite.database.sqlite.SQLiteException: not an error (code 0): Could not open the database in read/write mode.
And here is my code:
public void csr_test_1() throws Exception
{
DB_PATH=new File("/storage/sdcard1/sk2.db");
SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(DB_PATH, null);
String res = "";
Cursor c = db.rawQuery("SELECT synsetid, w2.lemma FROM sense LEFT JOIN word AS w2 ON w2.wordid=sense.wordid WHERE sense.synsetid IN (SELECT sense.synsetid FROM word AS w1 LEFT JOIN sense ON w1.wordid=sense.wordid WHERE w1.lemma='"+ "life" + "') AND w2.lemma<>'" + "life" + "'", null);
if( c!=null ){
boolean bRes;
for(bRes=c.moveToFirst(); bRes; bRes=c.moveToNext()){
String x = c.getString(0);
res = res + "." + x;
}
}else{
}
test_result("csr_test_1.1", res, ".one.two.three");
db.close();
test_result("csr_test_1.2", db_is_encrypted(), "unencrypted");
}
These are the permission i'm using
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
<uses-permission android:name="android.permission.EXPAND_STATUS_BAR" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="com.android.vending.BILLING" />
![enter image description here][1]
![enter image description here][2]
Please help me out... Thanks
This exception is thrown if sqlite3_db_readonly() returns non-zero. It can return non-zero if
the database file is read-only, or
the database file does not exist.
(Reference)
You have a hardcoded path "/storage/sdcard1/sk2.db" - it's likely a database does not exist there. Use variables from Environment to access your external storage instead of hardcoded paths.
add this permission to your AndroidManifest.xml.
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
please use both permission in manifest file
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
In case you still get this error even after you included:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
and you also implemented Runtime permissions then it almost certainly is a problem related to the way Android manages files from External SD Card. For more info check my answer here:
https://stackoverflow.com/a/46383795/1502079
if your sdk>N ,it must request write and read permission like this
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE},
1);
Related
i work on a app (> 4.4) and my boss would like to save the configuration of the APN into the SQLite (our database).
As this, if the APN is lost for a reason we can retrieve easily these information and configure it ..
well, i've try some code but actually NOTHING work ...
I have this error message
Java.Lang.SecurityException: No permission to write APN settings: Neither user 10081 nor current process has android.permission.WRITE_APN_SETTINGS.
Of course, i have added to the manifest the correct RIGTH as this
<uses-permission android:name="android.permission.READ_APN_SETTINGS"></uses-permission>
<uses-permission android:name="android.permission.WRITE_APN_SETTINGS"></uses-permission>
<uses-feature android:name="android.hardware.telephony" android:required="false" />
And here is it my code, it's with xamarin android but if you have some code with android , no matter i can find the translatation ..
Android.Net.Uri APN_TABLE_URI = Android.Net.Uri.Parse("content://telephony/carriers");
//path to preffered APNs
Android.Net.Uri PREFERRED_APN_URI = Android.Net.Uri.Parse("content://telephony/carriers/preferapn");
//
Android.Database.ICursor cursor = ctx.ContentResolver.Query(PREFERRED_APN_URI, null, null, null, null);
Thanks guys for all your time you spend here for to share your knowledge
try to add these two permissions:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
I am deleting all the images from the DCIM(CAMERA) directory after capturing images by getting path for the particular image.
It works fine while i am testing my app in Genymotion. But, When I tried it in android devices i.e. samsung s3 as well as in XOLO_a500s it did not deleting images captured.
What might be the Problem ?
Code to delete :
public void deleteCapturedImages() {
alCapturedImagesPath = mDBHelper.getCapturedImagesPath();
if (alCapturedImagesPath.size() > 0) {
for (int i = 0; i < alCapturedImagesPath.size(); i++) {
File f = new File(alCapturedImagesPath.get(i));
f.delete();
}
}
}
Permissions in Manifest :
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-feature android:name="android.hardware.camera" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
For that :
I am taking path of image captured, from the onActivityResult() of Camera and saving it to the database.
Now, At the time of deleting image, i am retrieving that path from database and converting it to File using new File(databasePath); as done in function deleteCapturedImages().
I am trying to read image paths from the storage and i keep getting the SecurityException here:
Caused by: java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaProvider uri content://media/external/images/media from pid=31409, uid=10053 requires android.permission.READ_EXTERNAL_STORAGE, or grantUriPermission()
I am compiling against api 23 and have the permissions clearly stated in my Manifest as a direct child of my Manifest. I understand as well that the new runtime permissions state you have to ask for permission during runtime when you want to access the storage. When i write this line that would begin the check for the permission i get a cannot resolve issue in the IDE:
ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE)
cannot resolve symbol Manifest.permission.READ_EXTERNAL_STORAGE. The only Manifest.permission that i can clearly state in my code is Manifest.permission.C2D_MESSAGE without getting a cannot resolve error. Is there something else i need to compile to have access to the other permissions in code?
Just to be clear this is the block of code that is giving me the SecurityException:
public List<String> getCameraImagesFull(Context context) {
Cursor cursor = null;
String[] star = {"*"};
ArrayList<String> result = new ArrayList<>();
cursor = context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, star, null, null, MediaStore.MediaColumns.DATE_ADDED + " DESC");
if (cursor != null) {
if (cursor.moveToFirst()) {
do {
String path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
result.add(path);
} while (cursor.moveToNext());
}
cursor.close();
}
return result;
}
this is the line that throws the exception:
cursor = context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, star, null, null, MediaStore.MediaColumns.DATE_ADDED + " DESC");
And here are all the permissions stated in my manifest:
<permission
android:name="com.example.gcm.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.USE_CREDENTIALS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="com.example.gcm.permission.C2D_MESSAGE" />
i am just wondering how i go about requesting that permission so i can get the image paths. This all works when i compile against any Lollipop build, just with the new runtime permissions on Marshmallow im getting the SecurityExeption. Any and all help is appreciated, thanks in advance
cannot resolve symbol Manifest.permission.READ_EXTERNAL_STORAGE
You need to add an import statement for android.Manifest, the same way you do with any other Java class.
The only Manifest.permission that i can clearly state in my code is Manifest.permission.C2D_MESSAGE without getting a cannot resolve error
There is no such value (C2D_MESSAGE) in android.Manifest.permission. I don't even know where there is a Manifest class, with a permission inner class, with a field named C2D_MESSAGE.
I have some problems in my App.
I just wanna read file in sdcard, so I put the file in /storage/sdcard/abcd (the folder I made by adb) in emulator.
File name is "1.14P", and I verify it is in that path. Its permission is 770.
But my problem is, I can't access sdcard by Application.
sampleFile = new File (Environment.getExternalStorageDirectory(), "abcd/1.14P");
sampleFile.exists() //return false
sampleFile2 = new File (Environment.getExternalStorageDirectory(), "tttt");
sampleFile2.mkdir() //return false
Sdcard is mounted well, I thought.
String state = Environment.getExternalStorageState(); //return "mounted"
I give the permission but I can't access to sdcard. Here is my manifest.xml. (part)
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.hu.test004" >
<uses-permission android:name="ANDROID.PERMISSION.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="ANDROID.PERMISSION.READ_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
...(Activities)
I can't understand why it happens. Please give me some favor. Thanks.
SOLVED::I've got it!
The problem is, I declared permissions in capital letter, so App doesn't grant permissions.
I change
<uses-permission android:name="ANDROID.PERMISSION.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="ANDROID.PERMISSION.READ_EXTERNAL_STORAGE"/>
to
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
and permission granted.
Thank you for your favor all.
I think the "," is part of the problem. The other is the "exists()" is for directories, not files.
See here:
new File(path) always actually creates a file on android?
contrary to the docs:
http://developer.android.com/reference/java/io/File.html
you may want to use isFile() instead.
Could you try adding "/" in front of your names??
e.g. "/abcd/1.14P"
in my app, the user can download different sound files. I use the downloadManager for this and everything is working fine here.
String url = localList.get(position).guid;
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
request.setDescription(localList.get(position).title);
request.setTitle("Downloading Podcast");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
request.setNotificationVisibility(DownloadManager.Request.
VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
}
request.setDestinationInExternalPublicDir("/MyAudio/",
localList.get(position).title + ".mp3");
DownloadManager manager = (DownloadManager)
v.getContext().getSystemService(Context.DOWNLOAD_SERVICE);
manager.enqueue(request);
The problem is, when i try to show all files in the directory with file.list or file.listfiles its returning NULL.
File f = new File("/MyAudio/");
File file[] = f.listFiles();
String filename[] = f.list();
I tried a lot of stuff, like changing the folder name and location, using request.setDestinationInExternalFileDir , writing and reading the folder with
File sdCard = Environment.getExternalStorageDirectory();
String folder = sdCard.getAbsolutePath() + "/MyAudio" ;
But nothing helped so far. I always get NULL returned by file.listFiles.
I use these permissions:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
which should cover everything i need.
One last point: I write the file in an arrayadapter class and try to read the folder from my mainactivity, and i see the folder and files on my phone, so im sure they exist.
Thx in advance