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().
Related
The story starts when my app get rejected from Play Store. I have a feature that user needs to take picture and upload to Server. So I have to take picture and save it somewhere in the storage area.
So I have these permissions in android manifest file
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature
android:name="android.hardware.camera"
android:required="true" />
<uses-feature
android:name="android.hardware.camera.autofocus" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<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" />
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="com.test.fileupload.provider"
android:exported="false"
android:grantUriPermissions="true">
<!-- ressource file to create -->
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="#xml/file_paths"></meta-data>
</provider>
For Android APi 30 + , there is a need to request MANAGE_EXTERNAL_STORAGE otherwise, it crashes .
<uses-permission
android:name="android.permission.MANAGE_EXTERNAL_STORAGE"
android:minSdkVersion="30" /
I save images to externalStorage.
val storageDir: File? = File( Environment.getExternalStorageDirectory().toString() +"/myimages")
Even I save photos in app's internal storage , MANAGE_EXTERNAL_STORAGE is required for API 30+. Otherwise it crashes.
Update -> I change it to
with
val storageDir: File? = File( context?.filesDir.toString() +"/my_images")
And some says we are supposed to use MediaStore . And I have tried
val mimeType = "image/*"
val values = ContentValues().apply {
put(MediaStore.Images.Media.DISPLAY_NAME, getNewFileName())
put(MediaStore.Images.Media.MIME_TYPE, mimeType)
put(
MediaStore.Images.Media.RELATIVE_PATH,
relativeLocation
)
}
val imageUri =
context.contentResolver.insert(MediaStore.Images.Media.getContentUri(MediaStore.VOLUME_EXTERNAL), values)
if (imageUri != null) {
currentPhotoPath = imageUri.toString()
shareUri = imageUri
}
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri)
The problem only happens when I try to convert imageUri to File .
imageUri from mediastore doesn't provide full path . I need actual file path so that I can upload to server.
Which library should I use to save image so that my app got approved by Google Playstore? It has been long week .Any comment/ solution is really appreciated .
I'm currently trying to displa an image from the storage, but I have no success.
What I'm dooing.
public Stream ReadImage(string path)
{
//var uri = Android.Net.Uri.Parse(path);
//var stream = MainActivity._activity.ContentResolver.OpenInputStream(uri);
//return stream;
var memoryStream = new MemoryStream();
using (var source = System.IO.File.OpenRead(path))
{
source.CopyTo(memoryStream);
}
return memoryStream;
}
string path = "/storage/emulated/0/DCIM/Camera/IMG_20190521_094202.jpg"
var imageSource = ImageSource.FromStream(
delegate {
return DependencyService.Get<IMediaHelper>().ReadImage(path);
});
But in the using line the programm doese not proceed, it does not reach the following lines and it does not load the file from the storage, the way with the contetn resolver does end teh same way, in the contentresolver line.
I have the Read/write external storage permission.
I am using android x, with Xamarin 5.0.0.2012. Under android 10 & and 11, on multiple different xiaomi devices.
This might be a bug, because I think, I had it running in an earlier project.
Some insight woult be appreciated, I'm biting my teath out on this nut.
To give users more control over their files and to limit file clutter, apps that target Android 10 (API level 29) and higher are given scoped access into external storage, or scoped storage, by default. Such apps have access only to the app-specific directory on external storage, as well as specific types of media that the app has created.You could read it here.
For android 10,you could request the requestLegacyExternalStorage attribute in your Application tag in the AndroidManifest.
<application android:label="NewForms.Android" android:resizeableActivity="true" android:requestLegacyExternalStorage="true" android:theme="#style/MainTheme">
For android 11,you need add <uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" /> in your AndroidManifest.And determine in the Activity whether the permission is enabled.
And you can't forget to dynamically request WRITE_EXTERNAL_STORAGE permissions (this was added after Android 6.0,named Runtime Permissions).
The final code is as follows:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="com.companyname.newforms" android:installLocation="preferExternal">
<uses-sdk android:minSdkVersion="18" android:targetSdkVersion="30" />
<application android:label="NewForms.Android" android:requestLegacyExternalStorage="true" android:theme="#style/MainTheme"></application>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />
</manifest>
and you could request the runtime permission like this in your MainActivity:
RequestPermissions(new string[] { Manifest.Permission.WriteExternalStorage}, 0);
if (Build.VERSION.SdkInt > BuildVersionCodes.Q)
{
if (!Environment.IsExternalStorageManager)
{
StartActivityForResult(new Intent( Android.Provider.Settings.ActionManageAllFilesAccessPermission), 101);
}
}
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"
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);
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