Skip to content
This repository was archived by the owner on May 7, 2022. It is now read-only.

Commit a57b78e

Browse files
committed
getPath implementation
1 parent 1a3fb8e commit a57b78e

File tree

11 files changed

+157
-22
lines changed

11 files changed

+157
-22
lines changed

.github/workflows/build.yml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
name: build
2+
3+
on:
4+
push:
5+
branches: [master]
6+
pull_request:
7+
branches: [master]
8+
9+
jobs:
10+
build-and-test:
11+
runs-on: ubuntu-latest
12+
13+
steps:
14+
- uses: actions/[email protected]
15+
16+
- name: Setup Node.js environment
17+
uses: actions/[email protected]
18+
with:
19+
node-version: 14.x
20+
21+
- name: Restore cache
22+
id: cache
23+
uses: actions/[email protected]
24+
with:
25+
path: node_modules
26+
key: ${{ runner.os }}-${{ hashFiles('yarn.lock') }}
27+
28+
- name: Install dependencies
29+
if: steps.cache.outputs.cache-hit != 'true'
30+
run: yarn
31+
32+
- run: yarn lint

README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,25 @@
11
# React Native Android URI Path
2+
3+
[![npm](https://img.shields.io/npm/v/@flyerhq/react-native-android-uri-path)](https://www.npmjs.com/package/@flyerhq/react-native-android-uri-path)
4+
[![build](https://github.com/flyerhq/react-native-android-uri-path/workflows/build/badge.svg)](https://github.com/flyerhq/react-native-android-uri-path/actions?query=workflow%3Abuild)
5+
[![Maintainability](https://api.codeclimate.com/v1/badges/b439310da19b3d6b3917/maintainability)](https://codeclimate.com/github/flyerhq/react-native-android-uri-path/maintainability)
6+
7+
Get an absolute path to a file retrieved by Android's `Intent.ACTION_GET_CONTENT` that returns a `content://` URI.
8+
9+
## Getting Started
10+
11+
```bash
12+
yarn add @flyerhq/react-native-android-uri-path
13+
```
14+
15+
## Usage
16+
17+
```ts
18+
import getPath from '@flyerhq/react-native-android-uri-path'
19+
20+
const path = getPath(uriString)
21+
```
22+
23+
## License
24+
25+
[MIT](LICENSE)
Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,45 @@
11
package chat.flyer.rnandroiduripath
22

3+
import android.net.Uri
4+
import android.provider.OpenableColumns
35
import com.facebook.react.bridge.ReactApplicationContext
46
import com.facebook.react.bridge.ReactContextBaseJavaModule
7+
import com.facebook.react.bridge.ReactMethod
8+
import okio.Okio
9+
import java.io.File
10+
import java.io.FileNotFoundException
11+
import java.io.IOException
512

613
class RNAndroidURIPathModule(reactContext: ReactApplicationContext) : ReactContextBaseJavaModule(reactContext) {
714

815
override fun getName() = "RNAndroidURIPath"
916

10-
override fun getConstants(): MutableMap<String, Any> {
11-
return hashMapOf("count" to 1)
17+
@ReactMethod(isBlockingSynchronousMethod = true)
18+
fun getPath(uriString: String): String {
19+
val uri = Uri.parse(uriString)
20+
val cursor = reactApplicationContext.contentResolver.query(uri, null, null, null, null)
21+
cursor ?: return ""
22+
23+
val nameIndex = cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME)
24+
cursor.moveToFirst()
25+
val name = cursor.getString(nameIndex)
26+
cursor.close()
27+
28+
try {
29+
val file = File.createTempFile("temp", name, reactApplicationContext.cacheDir)
30+
file.deleteOnExit()
31+
val stream = reactApplicationContext.contentResolver.openInputStream(uri)
32+
val source = Okio.buffer(Okio.source(stream))
33+
val sink = Okio.buffer(Okio.sink(file))
34+
sink.writeAll(source)
35+
sink.close()
36+
return file.absolutePath
37+
} catch (e: FileNotFoundException) {
38+
e.printStackTrace()
39+
} catch (e: IOException) {
40+
e.printStackTrace()
41+
}
42+
43+
return ""
1244
}
1345
}

example/ios/Podfile.lock

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,8 @@ PODS:
236236
- React-cxxreact (= 0.63.2)
237237
- React-jsi (= 0.63.2)
238238
- React-jsinspector (0.63.2)
239+
- react-native-document-picker (3.5.4):
240+
- React
239241
- React-RCTActionSheet (0.63.2):
240242
- React-Core/RCTActionSheetHeaders (= 0.63.2)
241243
- React-RCTAnimation (0.63.2):
@@ -337,6 +339,7 @@ DEPENDENCIES:
337339
- React-jsi (from `../node_modules/react-native/ReactCommon/jsi`)
338340
- React-jsiexecutor (from `../node_modules/react-native/ReactCommon/jsiexecutor`)
339341
- React-jsinspector (from `../node_modules/react-native/ReactCommon/jsinspector`)
342+
- react-native-document-picker (from `../node_modules/react-native-document-picker`)
340343
- React-RCTActionSheet (from `../node_modules/react-native/Libraries/ActionSheetIOS`)
341344
- React-RCTAnimation (from `../node_modules/react-native/Libraries/NativeAnimation`)
342345
- React-RCTBlob (from `../node_modules/react-native/Libraries/Blob`)
@@ -395,6 +398,8 @@ EXTERNAL SOURCES:
395398
:path: "../node_modules/react-native/ReactCommon/jsiexecutor"
396399
React-jsinspector:
397400
:path: "../node_modules/react-native/ReactCommon/jsinspector"
401+
react-native-document-picker:
402+
:path: "../node_modules/react-native-document-picker"
398403
React-RCTActionSheet:
399404
:path: "../node_modules/react-native/Libraries/ActionSheetIOS"
400405
React-RCTAnimation:
@@ -445,6 +450,7 @@ SPEC CHECKSUMS:
445450
React-jsi: 54245e1d5f4b690dec614a73a3795964eeef13a8
446451
React-jsiexecutor: 8ca588cc921e70590820ce72b8789b02c67cce38
447452
React-jsinspector: b14e62ebe7a66e9231e9581279909f2fc3db6606
453+
react-native-document-picker: 4921045c5c6c78243010f784eae0a3f4b2af7799
448454
React-RCTActionSheet: 910163b6b09685a35c4ebbc52b66d1bfbbe39fc5
449455
React-RCTAnimation: 9a883bbe1e9d2e158d4fb53765ed64c8dc2200c6
450456
React-RCTBlob: 39cf0ece1927996c4466510e25d2105f67010e13

example/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
},
1313
"dependencies": {
1414
"react": "^16.13.1",
15-
"react-native": "^0.63.2"
15+
"react-native": "^0.63.2",
16+
"react-native-document-picker": "^3.5.4"
1617
},
1718
"devDependencies": {
1819
"@babel/core": "^7.11.6",

example/src/App.tsx

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,37 @@
1-
import React, { useEffect } from 'react'
2-
import RNAndroidURIPath, { Counter } from 'react-native-android-uri-path'
1+
import getPath from '@flyerhq/react-native-android-uri-path'
2+
import React from 'react'
3+
import { Alert, Button, StyleSheet, View } from 'react-native'
4+
import DocumentPicker from 'react-native-document-picker'
35

46
const App = () => {
5-
useEffect(() => {
6-
console.log(RNAndroidURIPath)
7-
})
7+
const openPicker = async () => {
8+
try {
9+
const response = await DocumentPicker.pick({
10+
type: [DocumentPicker.types.allFiles],
11+
})
12+
Alert.alert('Path', getPath(response.uri), [{ text: 'OK' }], {
13+
cancelable: true,
14+
})
15+
} catch (err) {
16+
if (!DocumentPicker.isCancel(err)) {
17+
// Handle error
18+
}
19+
}
20+
}
821

9-
return <Counter />
22+
return (
23+
<View style={styles.constainer}>
24+
<Button title='Choose a file' onPress={openPicker} />
25+
</View>
26+
)
1027
}
1128

29+
const styles = StyleSheet.create({
30+
constainer: {
31+
alignItems: 'center',
32+
flex: 1,
33+
justifyContent: 'center',
34+
},
35+
})
36+
1237
export default App

example/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"noUnusedLocals": true,
1010
"noUnusedParameters": true,
1111
"paths": {
12-
"react-native-android-uri-path": ["../src"]
12+
"@flyerhq/react-native-android-uri-path": ["../src"]
1313
},
1414
"skipLibCheck": true,
1515
"strict": true,

example/yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5635,6 +5635,11 @@ react-is@^16.12.0, react-is@^16.8.1, react-is@^16.8.4, react-is@^16.8.6:
56355635
resolved "https://registry.yarnpkg.com/react-is/-/react-is-16.13.1.tgz#789729a4dc36de2999dc156dd6c1d9c18cea56a4"
56365636
integrity sha512-24e6ynE2H+OKt4kqsOvNd8kBpV65zoxbA4BVsEOB3ARVWQki/DHzaUoC5KuON/BiccDaCCTZBuOcfZs70kR8bQ==
56375637

5638+
react-native-document-picker@^3.5.4:
5639+
version "3.5.4"
5640+
resolved "https://registry.yarnpkg.com/react-native-document-picker/-/react-native-document-picker-3.5.4.tgz#4b9f261ea0d52e91d571670caaf55923ceb80f5f"
5641+
integrity sha512-ZKGAa8ztQ7zA1eE95OCiNsI/Q6fiq1Q3es8MyOEakBkWcX9avWNYaJUrbv/8v80Vo4RzcNxsO3wT6a2hgfpz7A==
5642+
56385643
react-native@^0.63.2:
56395644
version "0.63.2"
56405645
resolved "https://registry.yarnpkg.com/react-native/-/react-native-0.63.2.tgz#eaebf3430577b37fbd66ef228a86b3408259ef8e"

package.json

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
11
{
2-
"name": "react-native-android-uri-path",
2+
"name": "@flyerhq/react-native-android-uri-path",
33
"version": "1.0.0",
4-
"description": "",
4+
"description": "Get an absolute path to a file retrieved by Android's Intent.ACTION_GET_CONTENT that returns a content URI.",
55
"homepage": "https://github.com/flyerhq/react-native-android-uri-path#readme",
66
"main": "lib/index.js",
77
"types": "lib/index.d.ts",
88
"author": "Oleksandr Demchenko <[email protected]>",
99
"license": "MIT",
10+
"keywords": [
11+
"android",
12+
"react-native",
13+
"absolute-path",
14+
"intent",
15+
"resolver"
16+
],
1017
"files": [
1118
"android",
1219
"lib",
@@ -20,6 +27,9 @@
2027
"!gradlew.bat",
2128
"!local.properties"
2229
],
30+
"publishConfig": {
31+
"access": "public"
32+
},
2333
"scripts": {
2434
"compile": "rm -rf lib && tsc -p .",
2535
"lint": "eslint .",
@@ -48,14 +58,14 @@
4858
"react-native": "*"
4959
},
5060
"jest": {
51-
"preset": "react-native",
5261
"moduleFileExtensions": [
5362
"ts",
5463
"tsx",
5564
"js",
5665
"jsx",
5766
"json",
5867
"node"
59-
]
68+
],
69+
"preset": "react-native"
6070
}
6171
}

src/index.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { NativeModules, Platform } from 'react-native'
2+
3+
const getPath: (uriString: string) => string =
4+
Platform.OS === 'android'
5+
? NativeModules.RNAndroidURIPath.getPath
6+
: (uriString: string) => uriString
7+
8+
export default getPath

src/index.tsx

Lines changed: 0 additions & 8 deletions
This file was deleted.

0 commit comments

Comments
 (0)