I tried to upload text file in my phone(Android 11) to Gmail, Dropbox, Google Drive...
But, all failed.
I wrote android:preserveLegacyExternalStorage="true" tag in the manifest file.
TargetSdkVersion is 29.
Download file from above apps works fine. Only upload does not work.
It works for my old phone.(below Android 10)
I think, problem is caused by "Android 11 open failed: EACCES (Permission denied)"
This is my code. How can I solve this problem?
String state = Environment.getExternalStorageState();
if (state.equals(Environment.MEDIA_MOUNTED))
path = Environment.getExternalStorageDirectory().getAbsolutePath();
else
path = Environment.MEDIA_UNMOUNTED;
path = path + "/myFolder"; // /storage/emulated/0/myFolder
File dir = new File(path);
if (!dir.exists())
dir.mkdir();
..........................................................
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.setType("text/plain");
File file = new File(myFolder + "/" + "test.txt");
Uri uri = Uri.fromFile(file);
intent.putExtra(Intent.EXTRA_STREAM, uri);
Intent chooser = Intent.createChooser(intent, null);
if (intent.resolveActivity(getPackageManager()) != null)
startActivity(chooser);
Related
Good day. I am creating an application that downloads a file from firebase. Next, this file will have to open in the application that is installed in advance on the phone. How can I find this file after installation and open it in a new application. Thanks for any answer. sorry for my English
Download code:
public void downloadFiles(Context context, String fileName, String fileExtension, String destinationDirectory, String url) {
DownloadManager downloadManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
Uri uri = Uri.parse(url);
DownloadManager.Request request = new DownloadManager.Request(uri);
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setDestinationInExternalFilesDir(context, destinationDirectory, fileName + fileExtension);
downloadManager.enqueue(request);
What i tried.
open code:
public void openFile() {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setType("file/*");
File download = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS),"king.mcpack");
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(download));
startActivity(Intent.createChooser(intent, "Open with"));
}
i want to open a specific folder from menu drawer in android app but when i run the app it open recent files folder.
this is my code
if (id == R.id.action_downloaded) {
if (PermissionUtilities.isPermissionGranted(mActivity, PermissionUtilities.SD_READ_WRITE_PERMISSIONS, PermissionUtilities.REQUEST_READ_WRITE_STORAGE_DOWNLOAD)) {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
Uri uri = Uri.parse(Environment.getExternalStorageDirectory() + "/" + myfolder + "/");
intent.setDataAndType(uri, "application/*");
intent.addCategory(Intent.CATEGORY_OPENABLE);
startActivityForResult(Intent.createChooser(intent, "Select Pdf"), 1000);
}
Put as extra INITIAL_URI on your intent an uri the user choosed before with ACTION_GET_CONTENT.
I am receving this error: The file could not be accessed check your connection or make the filename shorter when trying to open a PDF file with Samsung j6, but I am able to open the same file with other android phone like Tecno.
Below is the code that I am using to open a PDF file.
File d = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
File pdfFile = new File(d, examID+".pdf");
Uri path = FileProvider.getUriForFile(this, BuildConfig.APPLICATION_ID + ".fileprovider", pdfFile);
Intent pdfIntent = new Intent(Intent.ACTION_VIEW);
pdfIntent.setDataAndType(path, "application/pdf");
pdfIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
pdfIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
try {
startActivity(pdfIntent);
} catch (ActivityNotFoundException e) {
Toast.makeText(DocExamView.this, "No Application available to view PDF", Toast.LENGTH_SHORT).show();
}
Trying to open pdf file from download folder on device,
i save the name of file in firebase and now i want to check if the file is already found on device and then open it else download from firebase.
File file = new File(Environment.getExternalStorageDirectory() + "filename");
System.out.println(Environment.getExternalStorageDirectory());
///but when i print Environment.getExternalStorageDirectory() i get
///storage/emulated/0
if (file.exists()) {
System.out.println(file.getAbsoluteFile());
Intent target = new Intent(Intent.ACTION_VIEW);
target.setDataAndType(Uri.fromFile(file), "application/pdf");
target = Intent.createChooser(target, "Open File");
target.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
Intent intent = Intent.createChooser(target, "Open File");
try {
startActivity(intent);
} catch (ActivityNotFoundException e) {
e.printStackTrace();
}
} else {
System.out.println("file not found");
}
try this to get your file from download directory
File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/" + filename);
Can someone tell me why this code is not saving the picture to the gallery anymore? I had it working at some point, then i changed something somewhere else, and now this doesn't work.
private void openImageIntent() {
// Determine Uri of camera image to save.
final File storageDir = Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
final String fname = ClassName.getUniqueImageFilename();
final File sdImageMainDirectory = new File(storageDir, fname);
outputFileUri = Uri.fromFile(sdImageMainDirectory);
// Camera.
final List<Intent> cameraIntents = new ArrayList<Intent>();
final Intent captureIntent = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
final PackageManager packageManager = getPackageManager();
final List<ResolveInfo> listCam = packageManager.queryIntentActivities(
captureIntent, 0);
for (ResolveInfo res : listCam) {
final String packageName = res.activityInfo.packageName;
final Intent intent = new Intent(captureIntent);
intent.setComponent(new ComponentName(res.activityInfo.packageName,
res.activityInfo.name));
intent.setPackage(packageName);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
cameraIntents.add(intent);
}
// Filesystem.
final Intent galleryIntent = new Intent();
galleryIntent.setType("image/*");
galleryIntent.setAction(Intent.ACTION_GET_CONTENT);
// Chooser of filesystem options.
final Intent chooserIntent = Intent.createChooser(galleryIntent,
"Chose a source");
// Add the camera options.
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS,
cameraIntents.toArray(new Parcelable[] {}));
startActivityForResult(chooserIntent, REQUEST_IMAGE_CAPTURE);
}
private static String getUniqueImageFilename() {
// TODO Auto-generated method stub
String fileName = "img_" + System.currentTimeMillis() + ".jpg";
return fileName;
}
This is the error i get:
10-29 23:53:50.692: E/BitmapFactory(12547): Unable to decode stream: java.io.FileNotFoundException: /file:/storage/emulated/0/DCIM/img_1414623222659.jpg: open failed: ENOENT (No such file or directory)
10-29 23:53:50.692: E/BitmapFactory(12547): Unable to decode stream: java.io.FileNotFoundException: /file:/storage/emulated/0/DCIM/img_1414623222659.jpg: open failed: ENOENT (No such file or directory)
final File storageDir = Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
You should put permission in Android Manifest to write to External Storage
Android storage is broken over different version of Android SDK. Your code will work on some devices but not all.
The System cannot create a file on system because of lack of permission so it is giving you error. The best way I have found is just to see that if external storage is mounted then create a file on external.
There are following two code snippets and they both will work.
Environment.getExternalStorageDirectory() + "/" + "myImages" + "/someimage.jpg";
After that you can start Media Store update Intent to update Android Gallery.
or Use this code
final File dir = new File(context.getFilesDir() + "/nfs/guille/groce/users/nicholsk/workspace3/SQLTest");
dir.mkdirs(); //create folders where write files
final File file = new File(dir, "BlockForTest.txt");