I am using image picker in react native but an error occurs:
file:///storage/emulated exposed beyond app through clipdata.item
On ios, the camera opens normally. and in android i can pick an image from gallery normally.
My code:
const selectFile = (itemI,inputValue) => {
let options = {
title: 'Select Image',
maxWidth:800,
maxHeight:800,
quality:0.2,
storageOptions: {
skipBackup: true,
path: 'images',
},
includeBase64: true,
saveToPhotos:true
};
ImagePicker.showImagePicker(options, (response) => {
console.log('Response = ', response);
if (response.didCancel) {
console.log('User cancelled image picker');
} else if (response.error) {
console.log('ImagePicker Error: ', response.error);
} else if (response.customButton) {
console.log(
'User tapped custom button: ',
response.customButton
);
alert(response.customButton);
} else {
//let source = response;
// You can also display the image using data:
let source = {
uri: 'data:image/jpeg;base64,' + response.data
};
filePath[itemI]=source;
addItemCustom(" ");
}
});
};
I tried to add:
<uses-feature android:name="android.hardware.camera.any" />
<uses-feature android:name="android.hardware.camera.autofocus" />
<uses-permission android:name="android.permission.CAMERA"/>
<uses-feature android:name="android.hardware.camera" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<application
....
android:requestLegacyExternalStorage="true"
...>
but the error is the same.
Related
I used for the first time react-native-ble-manager.
The app has all the permissions, however when I run the BleManager.scan() the app is closing without any error or logs.
I know the code is a little ugly but it's just for testing, the first second I run the bluetooth manager start and after 5 seconds I run the scan.
But the crash happen immediately after 5 seconds.
Any idea why?
import BleManager from 'react-native-ble-manager';
const BleManagerModule = NativeModules.BleManager;
const bleManagerEmitter = new NativeEventEmitter(BleManagerModule);
const handleDiscoverPeripheral = (peripheral) => {
console.log('Got ble peripheral', peripheral);
if (!peripheral.name) {
peripheral.name = 'NO NAME';
}
peripherals.set(peripheral.id, peripheral);
setList(Array.from(peripherals.values()));
}
const handleStopScan = () => {
console.log('Scan is stopped');
}
let interval;
let gImageShow = true;
let gTime = 0;
BleManager.enableBluetooth()
.then(() => {
// Success code
console.log("The bluetooth is already enabled or the user confirm");
})
.catch((error) => {
// Failure code
console.log("The user refuse to enable bluetooth");
});
function App(props) {
const [shouldShow, setShouldShow] = useState(gImageShow);
useEffect(() => {
interval = setInterval(() => {
if (gTime == 1)
{
BleManager.start({showAlert: true});
bleManagerEmitter.addListener('BleManagerDiscoverPeripheral', handleDiscoverPeripheral);
bleManagerEmitter.addListener('BleManagerStopScan', handleStopScan );
PermissionsAndroid.check(PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION).then((result) => {
if (result) {
console.log("Permission is OK");
}
});
}
if (gTime == 5)
{
BleManager.scan([], 10, true);
}
gImageShow = !gImageShow;
setShouldShow(gImageShow);
gTime = gTime + 1;
},1000);
return () => clearInterval(interval);
}, []);
useEffect(() => {
});
the manifest is like:
xmlns:tools="http://schemas.android.com/tools"
package="com.diagnostic_ble">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.BLUETOOTH" android:maxSdkVersion="30" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" android:maxSdkVersion="30" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" android:maxSdkVersion="28"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" tools:targetApi="Q"/>
<!-- Needed only if your app looks for Bluetooth devices. -->
<uses-permission android:name="android.permission.BLUETOOTH_SCAN" />
<uses-permission-sdk-23 android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
<uses-permission android:name="android.permission.BLUETOOTH_ADVERTISE" />
The problem has been solved. The new android 12 (I think from this one), needs a different permission that it hasn't been updated in react-native-ble-manager webpage.
In the manifest you need to add
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
<uses-feature android:name="android.hardware.bluetooth_le" android:required="true"/>
Also, In the app.js you need to call once requestPermissions();
that it can be like
const requestPermissions = async () => {
if (Platform.OS === 'android') {
const apiLevel = await DeviceInfo.getApiLevel();
if (apiLevel < 31) {
const granted = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
{
title: 'Location Permission',
message: 'Bluetooth Low Energy requires Location',
buttonNeutral: 'Ask Later',
buttonNegative: 'Cancel',
buttonPositive: 'OK',
},
);
} else {
const result = await requestMultiple([
PERMISSIONS.ANDROID.BLUETOOTH_SCAN,
PERMISSIONS.ANDROID.BLUETOOTH_CONNECT,
PERMISSIONS.ANDROID.ACCESS_FINE_LOCATION,
]);
const isGranted =
result['android.permission.BLUETOOTH_CONNECT'] ===
PermissionsAndroid.RESULTS.GRANTED &&
result['android.permission.BLUETOOTH_SCAN'] ===
PermissionsAndroid.RESULTS.GRANTED &&
result['android.permission.ACCESS_FINE_LOCATION'] ===
PermissionsAndroid.RESULTS.GRANTED;
}
} else {
}
};
The react native android permission pop-up is not showing, I cannot download a file. It works on a simulator but not on a real phone.
Here is my permission function :
const requestAndroidStoragePermission = async () => {
try {
const granted = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE,
{
title: "File access",
message:
"The app needs files access to download your performance.",
}
);
if (granted === PermissionsAndroid.RESULTS.GRANTED) {
this.setState({ storagePermission: true });
this.setState({ showExportModal: true });
} else {
console.log("Files permission denied");
}
} catch (err) {
console.warn(err);
}
};
I have the permission on my AndroidManifest.xml :
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
android:targetSdkVersion="29" />
Someone have an idea ?
I have added every thing required to download files to local storage still I'm getting this error
In Manifest
<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"/>
<uses-permission android:name="android.permission.ACCESS_MEDIA_LOCATION"/>
android:requestLegacyExternalStorage="true"
android:hardwareAccelerated="true"
In pubspec.yaml
permission_handler: ^6.1.1
In Build.gradle
compileSdkVersion 30
minSdkVersion 22
targetSdkVersion 30
Future<bool> _requestPermission(Permission permission, Permission permission3,
Permission permission4) async {
print("xxxx IN ");
if (await permission.isGranted) {
print('xxxx ohhh');
return true;
} else {
await permission.request();
.then((value) => () {
print("xxxx value" + value.toString());
if (value == PermissionStatus.granted) {
print("xxxx PERMISSION GRANTED ");
return true;
} else {
print("xxxx PERMISSION DENIED ");
}
})
.whenComplete(() => print('xxxx COMPLETED'))
.onError((error, stackTrace) => () {
print("xxxx onError " + error);
print("xxxx onError " + stackTrace.toString());
})
.catchError((onError) => () {
print('xxxx catchError ' + onError.toString());
});
print("xxxx OUT ");
// var result2 = await permission3.request();
// var result3 = await permission4.request();
}
return false;
}
from this function see what's printed
not getting printed from then()...
Please Please help me out :)
Use android:requestLegacyExternalStorage="true" in the application tag not in <activity> tag:
<application
android:requestLegacyExternalStorage="true" >
what is work for me this
permission_handler: ^8.3.0 // use this version
and in your code
final status = await Permission.storage.request();
var state = await Permission.manageExternalStorage.status;
var state2 = await Permission.storage.status;
if (status.isGranted) {
directory = (await getExternalStorageDirectory())!;
}
if (!state2.isGranted) {
await Permission.storage.request();
}
if (!state.isGranted) {
await Permission.manageExternalStorage.request();
}
if (!await directory!.exists()) {
await directory.create(recursive: true);
}
and in the my permissions
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE">
</uses-permission>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"></uses-
permission>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission
android:name="android.permission.ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION"/>
I need to add image picker in my app. I've created small class for image picker logic:
import {launchImageLibrary, launchCamera} from 'react-native-image-picker';
export default class ImagePickerService {
static openPickerLibrary(
setImageUrl: Dispatch<SetStateAction<string | null | undefined>>,
setModalVisible: Dispatch<SetStateAction<boolean>>,
) {
launchImageLibrary(
{mediaType: 'photo', includeBase64: true},
(response: any) => {
if (response.didCancel) {
console.log('User cancelled image picker');
} else if (response.error) {
console.log('ImagePicker Error: ', response.error);
} else {
setImageUrl(`data:image/jpeg;base64,${response.base64}`);
setModalVisible(false);
}
},
);
}
static openPickerCamera(
setImageUrl: Dispatch<SetStateAction<string | null | undefined>>,
setModalVisible: Dispatch<SetStateAction<boolean>>,
) {
launchCamera({mediaType: 'photo', includeBase64: true}, (response: any) => {
if (response.didCancel) {
console.log('User cancelled image picker');
} else if (response.error) {
console.log('ImagePicker Error: ', response.error);
} else {
setImageUrl(`data:image/jpeg;base64,${response.base64}`);
setModalVisible(false);
}
});
}
}
Then I call these methods this way:
<ModalButton
label="Take photo"
onPress={() =>
ImagePickerService.openPickerCamera(setImageUrl, setPickerVisible)
}
/>
<ModalButton
label="Choose photo"
onPress={() =>
ImagePickerService.openPickerLibrary(setImageUrl, setPickerVisible)
}
/>
Picker library works great. But picker camera doesn't open. Only the modal window closes and nothing else. Also there aren't any errors in console.
I don't understand why this happens. I have all permissions in AndroidManifest. Also I have tried this string and it didn't help:
android:requestLegacyExternalStorage="true"
Also I have tried these features and they also didn't help:
<uses-feature android:name="android.hardware.camera" android:required="false" />
<uses-feature android:name="android.hardware.camera.front" android:required="false" />
How can I fix this problem?
I solved my problem with react-native-camera.
I use geolocation to get the current location of users in my project. but I get the Timed Out Error error.
{TIMEOUT: 3, POSITION_UNAVAILABLE: 2, PERMISSION_DENIED: 1, message: "Location request timed out", code: 3}
API 28 android phone success,
API 21 android phone success,
API 23 android phone time out error.
All permission granted.
try{
Geolocation.getCurrentPosition(
position => {
const initialPosition = JSON.stringify(position);
this.setState({
firstLat: position.coords.latitude,
firstLng: position.coords.longitude,
});
this.getLocation(position.coords.latitude, position.coords.longitude);
this.setState({
isLoading:true
})
},
error => console.log(error),
{enableHighAccuracy: true, timeout:10000, maximumAge:1000},
);
}catch (e) {
console.log(e)
}
AndroidManifest.xml
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
thank you.