Writing file in USB Drive in android - android

I am trying to copy a file from my app to USB Drive(Pen drive) in Android but I am getting the Permission denied.
I have already given runtime storage permission.
Process: com.testapplication, PID: 19261
java.io.FileNotFoundException:
/mnt/runtime/write/02EE-0EE1/Tele-calling list.xlsx (Permission
denied)
at java.io.FileOutputStream.open0(Native Method)
Manifest
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_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.WRITE_MEDIA_STORAGE" />
Code
val cacheFile = File(cachepath, it.name)
val pendriveFile = File(pendrivepath, it.name)
Log.d(TAG, "********: " + cacheFile.path)
Log.d(TAG, "&&&&&&& " + pendriveFile.path)
cacheFile.copyTo(pendriveFile)
Toast.makeText(context, it.name + " copying..", Toast.LENGTH_SHORT).show()
I am facing issue in cacheFile.copyTo(pendriveFile) line.
Pendrive path is "/storage/02EE-0EE1" that I have calculated in runtime.

Related

Android 12 - CAMERA2 - Pictures are corrupted when saving

I found an anomaly during tests with this particular device, the details are below:
Hardware: samsung,SM-A326B (Smartphone Samsung A32)
OS: Android 12
ONE UI: 4.1
Google Play: 01/may/22
Patch level: 01/june/22
These are the specs of one device that works properly, the only differences that I see are related to different version of the patch level.
Hardware: samsung,SM-A515F (Smartphone Samsung A51)
OS: Android 12
ONE UI: 4.1
Google Play: 01/may/22
Patch level: 01/may/22
I have implemented a customization of camera2 in my application, this always works except with this device, the specific anomaly is that the images are saved completely white and are corrupted, the image can only be recovered by third party software.
These are the permissions I ask for in the manifest:
<uses-feature android:name="android.hardware.location.gps " />
<uses-feature android:name="android.hardware.location.network" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" tools:ignore="ScopedStorage" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />
This is the code that I use to save the images
public void saveCameraImage(Image mImage, File mFile, String mOrientation, Context context) {
ByteBuffer buffer = mImage.getPlanes()[0].getBuffer();
byte[] bytes = new byte[buffer.remaining()];
buffer.get(bytes);
FileOutputStream fileOutputStream = null;
try {
fileOutputStream = new FileOutputStream(mFile);
fileOutputStream.write(bytes);
} catch (IOException e) {
wil.WriteFile("saveImage - Exception: " + e.getMessage(), context);
} finally {
mImage.close();
if (null != fileOutputStream) {
try {
fileOutputStream.close();
} catch (IOException e) {
wil.WriteFile("saveImage - Exception: " + e.getMessage(), context);
} finally {
saveExifData(mFile.getPath(), context);
createThumbs(mOrientation, mFile, context);
}
}
}
}
This is the function that I named "saveExifData"
private void saveExifData(String filepath, Context context) {
try {
if (filepath.endsWith(IMAGE_FORMAT)) {
File file = new File(filepath);
if (file.exists()) {
DbGest dbGest = DbGest.getInstance(context);
String dateTime = dbGest.getTimestampFromFileToSend(filepath, "dateTime", context);
String dateTimeGps = dbGest.getTimestampFromFileToSend(filepath, "gps", context);
String dateTimeDesc = dbGest.getTimestampFromFileToSend(filepath, "dateTimeDesc", context);
ExifInterface exifInterface = new ExifInterface(filepath);
exifInterface.setAttribute(ExifInterface.TAG_COPYRIGHT, "xxxxx ");
exifInterface.setAttribute(ExifInterface.TAG_MODEL, Build.MANUFACTURER + "," + Build.MODEL);
exifInterface.setAttribute(ExifInterface.TAG_SOFTWARE, dbGest.getSetting("appName", context));
exifInterface.setAttribute(ExifInterface.TAG_DATETIME, dateTime);
exifInterface.setAttribute(ExifInterface.TAG_DATETIME_ORIGINAL, dateTime);
exifInterface.setAttribute(ExifInterface.TAG_USER_COMMENT, "Immagine scattata tramite " + dbGest.getSetting("appName", context) + " il " + dateTimeDesc);
CoordinatesData position = DbGest.getInstance(context).getBetterPositionKnown(context);
exifInterface.setAttribute(ExifInterface.TAG_GPS_LATITUDE, String.valueOf(position.getLat()));
exifInterface.setAttribute(ExifInterface.TAG_GPS_LONGITUDE, String.valueOf(position.getLng()));
exifInterface.setAttribute(ExifInterface.TAG_GPS_DATESTAMP, dateTimeGps);
exifInterface.setAttribute(ExifInterface.TAG_GPS_SPEED, String.valueOf(position.getSpeed()));
exifInterface.setLatLong(position.getLat(), position.getLng());
exifInterface.saveAttributes();
}
}
} catch (Exception e) {
wil.WriteFile("saveExifData - Exception: " + e.getMessage(), context);
}
}
So I guess it's not a permission issue, also there are no exceptions, so I don't know how to fix this
After several test and debugging I find the cause of the bug.
The problem it is in the "saveExifData", for some reason this row of code
exifInterface.setAttribute(ExifInterface.TAG_COPYRIGHT, "xxxx");
it is the cause of the problem

file deletion operation getting failed on the SD card

Android file deletion operation getting failed on the SD card .
following is my code .
File testingfile = new File("file path in SD card");
try{
boolean bool = testingfile.delete();
}
catch(){
Log.d (TAG, e.printmessage());
}
Permissions (already granted ):-
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
the file is not getting deleted . the bool always return false.
My buildToolsVersion "28.0.3"

Create folder in internal storage in android using kotlin

I want to create a folder in the internal storage of an android device where I can save my .csv files. But I am unable to create a folder in the internal storage.
Below is my code snippet for creating the folder
fun getStorageDirectory(): File {
if (Environment.getExternalStorageState() == null) {
val f = File(Environment.getDataDirectory().absolutePath + "/Afolder/")
if (!f.exists())
f.mkdirs()
return f
} else {
val f = File(Environment.getExternalStorageDirectory().absolutePath + "/Afolder/")
if (!f.exists())
f.mkdirs()
return f
}
}
and this is the permissions I used in my manifest.
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

android application failed to create directory in SD card

I'm trying to create a directory in an SD card using the below code.
String state = Environment.getExternalStorageState();
if (state.equals(Environment.MEDIA_MOUNTED)) {
String secondaryStore = System.getenv("SECONDARY_STORAGE");
File videoDirectory = new File(secondaryStore + File.separator +"video_files");
if(!videoDirectory.exists()){
boolean sd_mkdirs = videoDirectory.mkdirs();
if(sd_mkdirs){
Log.d(TAG,"file Created");
}
}
}else{
File videoDirectory = new File(Environment.getExternalStorageDirectory() + File.separator + "video_files");
if(!videoDirectory.exists()){
boolean internal_mkdirs = videoDirectory.mkdirs();
if(internal_mkdirs){
Log.d(TAG,"file Created");
}
}
}
This code checks whether or not the SD card is mounted. If true It creates a directory in the SD card, otherwise in internal storage. However, when it is suppossed to make a directory on the SD card _mkdirs returns false. What am I doing wrong ?
This is my AndroidManifest file. I think I included the permission to write.
<?xml version="1.0" encoding="utf-8"?>
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />

Copy file to external sdcard android

I cannot copy a file to the external sdcard. Does not show up any error, in-fact shows success, but the file is not on the sd card. Code is as follows:
window.resolveLocalFileSystemURI(fileURI, step1,fail);
function step1(tmp_file)
{
file = tmp_file;
window.resolveLocalFileSystemURI("file:///mnt/extsd", step2,fail); //resolve destinaion
}
function step2(destination)
{
file.moveTo(destination,"example.jpg",move_success, move_fail);
}
So on the end it calls move_success.
NOTE: IT WORKS IF I CHANGE PATH FROM 'file:///mnt/extsd' to the internal sdcard path 'file:///mnt/sdcard'
Permissions in manifest
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
You should build your path, not define it as default, cause not in all devices the sdcard is called "sdcard", for example, i have a chinnese tablet, and this has 2 sdcard slots, so i have to use the path "sdcard2" you can do it:
File sdcard = Environment.getExternalStorageDirectory();
String path = "file://"+sdcard.getAbsolutePath();
Then you can use the variable
window.resolveLocalFileSystemURI(path, step2,fail); //resolve destinaio
Or, you can use this method to copy a file:
public void copy(File src, File dst) throws IOException {
try {
InputStream in = new FileInputStream(src);
OutputStream out = new FileOutputStream(dst);
// Transfer bytes from in to out
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
in.close();
out.close();
} catch (IOException io) {
Toast.makeText(this, "Error: " + io, Toast.LENGTH_LONG).show();
}
}
You can copy the file, next delete it, as a fast move.
Or as other alternative, you can use the property renameTo() of the file,
For example:
File sdcard = Environment.getExternalStorageDirectory();
File example= new File(sdcard.getAbsolutePath()+"/example.txt");
File newpath= new File(sdcard.getAbsolutePath()+"/examplefolder/example.txt");
example.renameTo(newpath);//this will move the file to the new path

Categories

Resources