Can't Read Files in Sdcard in emulator - android

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"

Related

Xamarin android display image from external Storage

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);
}
}

How to delete files from media content providers on android N

This code worked on previous versions of android, but no longer seems to work on N.
I want to delete a media file, given the content URI of the file. I query the content provider for the path of the file. On android N, this seems to return a filename of the following form:
/storage/1143-1403/DCIM/Camera/20171019_185604.mp4
On previous versions of Android, the path would been a direct path to the SD Card. (And the real file can actually be found in the /sdcard/DCIM/Camera directory).
The problem: when I call new File(path).delete(), the delete fails. It succeeds in previous versions of Android.
Manifest permissions are:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_INTERNAL_STORAGE" />
I have requested the following permissions, and they have been granted:
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.READ_EXTERNAL_STORAGE,
...
Manifest.permission.MODIFY_AUDIO_SETTINGS,
There is no Manifest.permission flag for WRITE_INTERNAL_STORAGE.
I'm guessing "/storage/1143-1403/DCIM/Camera" is some kind of fused file system.
Calling file = file.getAbsoluteFile().getCanonicalFile(); does not change the path or otherwise improve the situtation.

Mkdirs does not create folder in internal storage (Android)

I'm trying to create a folder in the internal storage of an app so I can download files to it. But it doesn't seem to work. I've been looking for a solution for hours and hours but I can't find one and it's starting to drive me crzay.
This the code to create the folder:
String intStorageDirectory = Environment.getDataDirectory().toString();
File folder = new File(intStorageDirectory, "testthreepdf");
folder.mkdirs();
This code always returns "DOES NOT EXIST":
File f = new File(Environment.getDataDirectory() + "/testthreepdf");
if(f.exists())
{
Log.i("testApp", f.toString() + "EXISTS");
}
else {
Log.i("testApp", f.toString() + " DOES NOT EXIST");
}
These are the android permissions:
<uses-permission android:name="android.permission.INTERNET" >
</uses-permission>
<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_INTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" >
</uses-permission>
<uses-permission android:name="android.permission.READ_PHONE_STATE" >
</uses-permission>
Anybody know what's wrong with this code?
Environment.getDataDirectory() returns the top-level data folder shared by all apps. You can't create folders there. To create folders in your app's data folder, use getFilesDir() instead, like this:
String intStorageDirectory = getFilesDir().toString();
File folder = new File(intStorageDirectory, "testthreepdf");
folder.mkdirs();
You need a Context to call getFilesDir().
For more information about the difference between getDataDirectory() and getFilesDir(), see this StackOverflow question.
Another problem (though not specifically causing any bug) is that READ_INTERNAL_STORAGE and WRITE_INTERNAL_STORAGE are non-existent in Android. They have no effect and should be removed from your manifest.

How to display html page kept on sdcard on webview

I am not able to display html page kept in sdcard on webview. I have tried using this two methods:
webPage.loadUrl("file://"+ Environment.getExternalStorageDirectory()+"/page1.html");
and
webPage.loadUrl("content://com.android.htmlfileprovider"+
Environment.getExternalStorageDirectory()+"/page1.html");
Please Guide.
Try This.
webPage.loadUrl("file://"+Environment.getExternalStorageDirectory()+"/page1.html");
You forgot to add slash.
Add Internet Permission and Read External Permission.
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

Why Images.Media.insertImage doesn't require WRITE_EXTERNAL_STORAGE permission

I realize even I do not have
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
in my `AndroidManifest.xml
String path = Images.Media.insertImage(((Activity)MainView.this.getContext()).getContentResolver(), screenCaptureBitmap, "Title", "Description");
still able to write image file to location /mnt/sdcard/DCIM/Camera. May I know why is it so?
maybe in depend on the content provider MediaStore, you through it to insert, it's a RPC invoke. and the MediaStore provider in source code: packages/providers/MediaProvider, in it's manifest define the permission
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />,
may be it's the reason.

Categories

Resources