java.io.FileNotFoundException: /sdcard/...: open failed: EISDIR (Is a directory) - java

hello I'm trying to upload a file to my application and on some devices for uploading from sdcard it gives me an error:java.io.FileNotFoundException: (path in sdcard): open failed: EISDIR (Is a directory). anybody have any idea why?? My code:
browsing file and opening file manager:
private void doBrowseFile() {
Intent chooseFileIntent = new Intent(Intent.ACTION_GET_CONTENT);
chooseFileIntent.setType("application/pdf");
// Only return URIs that can be opened with ContentResolver
chooseFileIntent.addCategory(Intent.CATEGORY_OPENABLE);
chooseFileIntent = Intent.createChooser(chooseFileIntent, "Choose a file");
startActivityForResult(chooseFileIntent, UNIQUE_REQUEST_CODE);
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == UNIQUE_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
if (data != null) {
Uri fileUri = data.getData();
Log.i(LOG_TAG, "Uri: " + fileUri);
String filePath = null;
try {
filePath = FileUtils.getPath(this, fileUri);
} catch (Exception e) {
Log.e(LOG_TAG, "Error: " + e);
Toast.makeText(this, "Error: " + e, Toast.LENGTH_SHORT).show();
}
getBase64FromPath(filePath);
}
}
}
super.onActivityResult(requestCode, resultCode, data);
}
Encoding file from file path:
#RequiresApi(api = Build.VERSION_CODES.O)
public void getBase64FromPath(String path) {
String base64 = "";
try {
File file = new File(path);
byte[] buffer = new byte[(int) file.length() + 100];
file.getParentFile().mkdirs();
FileInputStream fileInputStream = new FileInputStream(file); //THIS LINE GIVES ME ERROR
#SuppressWarnings("resource")
int length = fileInputStream.read(buffer);
base64 = Base64.encodeToString(buffer, 0, length,
Base64.DEFAULT);
uploadFile(base64);
} catch (IOException e) {
Toast.makeText(this, "error:" + e.getMessage() , Toast.LENGTH_SHORT).show();
}
}
If anybody know any idea why please tell. It gives this error on some devices only. on the others it works perfectly fine. thanks.

Get rid of FileUtils.getPath(), as it will never work reliably.
You can get an InputStream on your content by calling getContentResolver().openInputStream(fileUri). You can then use that InputStream instead of the FileInputStream to read in your content.
Note that your app will crash with an OutOfMemoryError with large PDFs. I strongly recommend that you find some way to upload your content without reading it all into memory at once. For example, you might consider not converting it to base-64.

Related

How to write multiples files to user accessible directories on an Android device?

In my Android Studio project I want to implement a function, where 3 files should get exported. So I want the user to choose a directory and enter the name for a new directory, in which the files are going to be stored.
Right now, I already have an intent which lets the user choose, where to place the new folder and how to name the new folder:
Intent intent = new Intent(Intent.ACTION_CREATE_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType(DocumentsContract.Document.MIME_TYPE_DIR);
intent.putExtra(Intent.EXTRA_TITLE, getString(R.string.folder_backup));
startActivityForResult(intent, REQUEST_CODE_SAVE_BACKUP);
In the onActivityResult method, I tried to save the 3 files. Here is the code:
#Override
public void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE_SAVE_BACKUP
&& resultCode == Activity.RESULT_OK) {
Uri uri = null;
if (data != null) {
uri = data.getData();
String dbFileName = ExampleDatabase.DB_NAME;
List<String> dbComponentsNames = new ArrayList<>();
dbComponentsNames.add(dbFileName);
dbComponentsNames.add(dbFileName + "-shm");
dbComponentsNames.add(dbFileName + "-wal");
try {
for (int i = 0; i < dbComponentsNames.size(); i++) {
uri = Uri.parse(uri.toString() + "%2F" + dbComponentsNames.get(i));
File dbComponent = getActivity().getDatabasePath(dbComponentsNames.get(i));
byte[] byteArray = new byte[(int) dbComponent.length()];
FileInputStream fileInputStream = new FileInputStream(dbComponent);
fileInputStream.read(byteArray);
ParcelFileDescriptor pfd = getActivity().getContentResolver().openFileDescriptor(uri, "w");
FileOutputStream fileOutputStream = new FileOutputStream(pfd.getFileDescriptor());
fileOutputStream.write(byteArray);
pfd.close();
}
} catch (Exception e) {
}
}
}
}
My idea was to get the URI from the created folder and just append the file names to that URI, so these files get stored in the newly created folder. I also found out, that a \ in the URI is replaced by %2F but this doesn't matter. Does anyone know, how to achieve saving multiple files without using MediaStore?

AndroidStudio getExternalStoragePublicDirectory in API 29

In API 29, getExternalStoragePublicDirectory was deprecated so that I have to find way to convert the following code to API 29
String pathSave = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)
+ new StringBuilder("/GroupProjectRecord_")
.append(new SimpleDateFormat("dd-MM-yyyy-hh_mm_ss")
.format(new Date())).append(".3gp").toString();
Thanks for your help!
As mentioned in android docs
Apps can continue to access content stored on shared/external storage
by migrating to alternatives such as
Context#getExternalFilesDir(String)
Try with this method.
public void getFilePath(Context context){
String path = context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS)
+ new StringBuilder("/GroupProjectRecord_")
.append(new SimpleDateFormat("dd-MM-yyyy-hh_mm_ss")
.format(new Date())).append(".3gp").toString();
Log.d(TAG, "getFilePath: "+path);
}
In API 29 and above doing anything with Paths outside of the apps private storage won't work.
See https://developer.android.com/training/data-storage/files/external-scoped for details
So to save you need to do something like:-
// OnClick Save Button
public void Save(View view){
// Ask for a new filename
Intent intent = new Intent(Intent.ACTION_CREATE_DOCUMENT);
// Restrict to openable items
intent.addCategory(Intent.CATEGORY_OPENABLE);
// Set mimeType
intent.setType("text/plain");
// Suggest a filename
intent.putExtra(Intent.EXTRA_TITLE, "text.txt");
// Start SAF file chooser
startActivityForResult(intent, 1);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent resultData) {
super.onActivityResult(requestCode, resultCode, resultData);
if (requestCode == 1 && resultCode == RESULT_OK) {
Log.d("SAF", "Result code 1");
if (resultData != null) {
Uri uri = resultData.getData();
Log.d("SAF", uri.toString());
// Now write the file
try {
ParcelFileDescriptor pfd =
this.getContentResolver().
openFileDescriptor(uri, "w");
// Get a Java FileDescriptor to pass to Java IO operations
FileDescriptor fileDescriptor = pfd.getFileDescriptor();
// Read Input stream
FileOutputStream fileOutputStream =
new FileOutputStream(fileDescriptor);
// .....
} catch (Exception e){
// Do something with Exceptions
}
} else {
Log.d("SAF", "No Result");
}
}
}

Reduce image file size from camera crop

I am trying to implement the best approach to reduce a file, basicly my app is about plants, i need high quality images, so the first thing i did was use a external library to crop images in thumbnail(so i can mantain aspect ratio) i use 1:1 thumnbnail format.
Every time i take a picture and if everything is ok on activityResult, it calls the builded crop library so i can cut and get the uri, like this:
protected void onActivityResult(int requestCode, int resultCode, final Intent data) {
super.onActivityResult(requestCode, resultCode, data);
dateTimer = getTime();
Log.d("codig",String.valueOf(requestCode));
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
Log.d("resultOK","resultOK");
CropImage.activity(data.getData())
.setAspectRatio(1,1)
.setInitialCropWindowPaddingRatio(0)
.setActivityTitle("Corte a foto")
.setActivityMenuIconColor(R.color.nephritis)
.setRequestedSize(300, 300, CropImageView.RequestSizeOptions.RESIZE_INSIDE)
.start(this);
}
if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
CropImage.ActivityResult result = CropImage.getActivityResult(data);
if (resultCode == RESULT_OK) {
Uri uri = result.getUri();
Bitmap pic = null;
try {
pic = MediaStore.Images.Media.getBitmap(this.getContentResolver(), uri);
} catch (IOException e) {
e.printStackTrace();
}
path = saveToInternalStorage(pic);
Log.d("caminho",path);
showDetailsDialog(data);
} else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) {
Exception error = result.getError();
Log.d("pict12",error.toString());
}
}
}
as you guys can see i have a setRequestSize method there, where i reduce the size of the image (this works, but just sometimes, the text i send as base64 is still to big.
i convert the uri to bitmap and save it on the device file, on next acitivty i get the file
private String saveToInternalStorage(Bitmap bitmapImage){
ContextWrapper cw = new ContextWrapper(getApplicationContext());
// path to /data/data/yourapp/app_data/imageDir
File directory = cw.getDir("imageDir", Context.MODE_PRIVATE);
// Create imageDir
File mypath=new File(directory,"captured.jpg");
FileOutputStream fos = null;
try {
fos = new FileOutputStream(mypath);
// Use the compress method on the BitMap object to write image to the OutputStream
bitmapImage.compress(Bitmap.CompressFormat.JPEG, 100, fos);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return directory.getAbsolutePath();
}
so on next activity i get it like this:
private void loadImageFromStorage(String path)
{
try {
File f=new File(path, "captured.jpg");
Log.d("filehe",f.toString());
b = BitmapFactory.decodeStream(new FileInputStream(f));
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
}
i get the b(bitmap) and covert him to byte[] after to base64(what my server asks for)
byte[] capture = encodeImage(b);
String encodedImage = Base64.encodeToString(capture,1);
Log.d("encodedImage",encodedImage);
params.put("base64", encodedImage);
the problem here is that i need to preserve the quality of the image and i can't get the quality without increasing the heavyness on my server, can't i compress it somehow? i do it on the file but doesn't work

in Android API 23 - why can I not programmatically alter ringtones?

I am trying to programmatically change a ringtone in API 23.
I have looked at a lot of examples on Stack Overflow and they all seem to "work" (i.e. not crash) but they do not "work" as in the mp3 doesn't become the ringtone - instead the ringtone becomes simply zero noise. So obviously /something/ happened. (no crash, now no ringtone noise)
I have split this off into a small side project to try to isolate it because it's driving me up the wall - I hope maybe you guys can see something I can't!
I have:
placed the mp3 in \res\raw
I have verified I can play the mp3 fine with this code
MediaPlayer mPlayer = MediaPlayer.create(me, R.raw.meepmeep);
mPlayer.start();
added <uses-permission android:name="android.permission.WRITE_SETTINGS"/> to the manifest
managed the api 22 permission scenario in java
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && !Settings.System.canWrite(this)) {
new AlertDialog.Builder(this)
.setMessage("Please Assign Meep Meep Write Permissions")
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(android.provider.Settings.ACTION_MANAGE_WRITE_SETTINGS);
intent.setData(Uri.parse("package:" + getPackageName()));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
try {
startActivity(intent);
} catch (Exception e) {
Log.e("MainActivity", "error starting permission intent", e);
}
}
})
.show();
return;
}
used a method to carefully fetch the mp3 path from the assets folder
public String LoadFile(String fileName, boolean loadFromRawFolder) throws IOException
{
InputStream iS;
if (loadFromRawFolder)
{
int rID = resources.getIdentifier("meep.example.com.meep:raw/"+fileName, null, null);
iS = resources.openRawResource(rID);
}
else
{
iS = resources.getAssets().open(fileName);
}
byte[] buffer = new byte[iS.available()];
iS.read(buffer);
ByteArrayOutputStream oS = new ByteArrayOutputStream();
oS.write(buffer);
oS.close();
iS.close();
return oS.toString();
}
tried to carefully copy the file to the local storage (as a desperate attempt to get it to work as well as tried to assign it from the raw assets lib
String path = "";
try {
LoadFile("meepmeep", true);
} catch (IOException e) {
//display an error toast message
Toast toast = Toast.makeText(me, "File: not found!", Toast.LENGTH_LONG);
toast.show();
}
//copy file to device
File newSoundFile = new File(path);
//Uri mUri = Uri.parse("android.resource://meep.example.com.meep/R.raw.meepmeep");
Uri mUri = MediaStore.Audio.Media.getContentUriForPath(newSoundFile.getAbsolutePath());
ContentResolver mCr = getContentResolver();
AssetFileDescriptor soundFile;
try {
soundFile= mCr.openAssetFileDescriptor(mUri, "r");
try {
byte[] readData = new byte[1024];
FileInputStream fis = soundFile.createInputStream();
FileOutputStream fos = new FileOutputStream(newSoundFile);
int i = fis.read(readData);
while (i != -1) {
fos.write(readData, 0, i);
i = fis.read(readData);
}
fos.close();
} catch (IOException io) {
}
} catch (FileNotFoundException e) {
soundFile=null;
}
ContentValues values = new ContentValues();
values.put(MediaStore.MediaColumns.DATA, newSoundFile.getAbsolutePath());
values.put(MediaStore.MediaColumns.TITLE, "Meep Meep");
values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
values.put(MediaStore.MediaColumns.SIZE, newSoundFile.length());
values.put(MediaStore.Audio.Media.ARTIST, "RoadRunner");
//values.put(MediaStore.Audio.Media.DURATION, 230);
values.put(MediaStore.Audio.Media.IS_RINGTONE, true);
values.put(MediaStore.Audio.Media.IS_NOTIFICATION, true);
values.put(MediaStore.Audio.Media.IS_ALARM, true);
values.put(MediaStore.Audio.Media.IS_MUSIC, false);
//Insert it into the database
Uri uri = MediaStore.Audio.Media.getContentUriForPath(newSoundFile.getAbsolutePath());
Uri newUri = mCr.insert(uri, values);
try {
RingtoneManager.setActualDefaultRingtoneUri(
me,
RingtoneManager.TYPE_RINGTONE,
newUri
);
}
catch (Throwable t){
setMessage("meepmeep error");
}
setMessage("meepmeep set");
}
but nothing seems to work. It always fails becuase either soundFile= mCr.openAssetFileDescriptor(mUri, "r"); returns null or if I decline to use that code block and try to change ringtone direct from \res\raw\ folder then it simply gives a blank sound for ringtone.
I am totally stuck for ideas?

Android PDF Load Timing

The requirements: ensure that the PDF document is deleted from the device after the user has left the PDF viewing screen
The problem: on certain devices (Samsung 4.4.2 and Samsung 4.1.2 for sure, but not Asus 4.2.1) only the first time that the PDF is requested after restarting the application an error message is displayed stating "This document cannot be opened". Thereafter the PDF will load normally. I'm thinking this is a timing issue due to processes that need to be started the first time, but are running after the first attempted load.
The code: note that createFile() is called first, then startActivityForIntentResult()
private File file;
private ArrayList<Uri> uriList = new ArrayList<Uri>();
private void createFile() {
int fileNameLength = pdfFileName[0].length();
String fileName = pdfFileName[0].substring(0, fileNameLength - 4) + DateTime.now();
String fileExtension = pdfFileName[0].substring(fileNameLength - 4, fileNameLength);
byte[] content = Base64.decodeBase64(pdfData[0].getBytes());
BufferedOutputStream outputStream = null;
try {
File path = new File(getExternalFilesDir(null).getAbsolutePath(), "temp");
if (!path.exists()) {
path.mkdirs();
}
file = new File(path, fileName + fileExtension);
outputStream = new BufferedOutputStream(new FileOutputStream(file));
outputStream.write(content);
file.deleteOnExit();
uriList.add(Uri.fromFile(file));
}
catch (FileNotFoundException ex) {
ex.printStackTrace();
}
catch (IOException ex) {
ex.printStackTrace();
}
finally {
try {
if (outputStream != null) {
outputStream.flush();
outputStream.close();
}
}
catch (IOException ex) {
ex.printStackTrace();
}
}
}
private static int REQUEST_CODE = 1;
private Intent intent;
private void startActivityForIntentResult() {
if (file.exists()) {
Uri targetUri = uriList.get(0);
intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(targetUri, "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
try {
startActivityForResult(intent, REQUEST_CODE);
}
catch (ActivityNotFoundException e) {
toastTitle = "Error Displaying PDF";
toastMessage = "Please make sure you have an application for viewing PDFs installed on your device and try again.";
toast = new GenericCustomToast();
toast.show(toastTitle, toastMessage, QueryForPDF.this);
}
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (resultCode == RESULT_CANCELED && requestCode == REQUEST_CODE) {
if(!file.delete()) {
file.delete();
}
}
searchAgain();
}
#Override
public void onBackPressed() {
super.onBackPressed();
if(!file.delete()) {
file.delete();
}
searchAgain();
}
#Override
public void onStop() {
super.onStop();
if(!file.delete()) {
file.delete();
}
}
#Override
public void onDestroy() {
super.onDestroy();
if(!file.delete()) {
file.delete();
}
}
EDIT: I have also tried implementing a callback to be absolutely certain that createFile() has finished it's work. I even tried adding delays (of different time increments) as well as adding (the completely unnecessary) flags for Intent.FLAG_GRANT_READ_URI_PERMISSION, Intent.FLAG_GRANT_WRITE_URI_PERMISSION, and Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION.
I still don't know why this works, but here's the solution in case anyone else runs into this issue:
It's the directory where the file is created. For some reason on the two Samsung devices there was something different in how the files were either accessed or created versus the Asus device. So File path = new File(getExternalFilesDir(null).getAbsolutePath(), "temp"); becomes File path = new File(getExternalCacheDir().getAbsolutePath()); and the problem goes away.

Categories

Resources