React Native Android: 删除文件

huangapple go评论58阅读模式
英文:

React Native Android: Deleting file

问题

我正在制作一个画廊样式的应用程序,在这个应用程序中,我需要添加删除图像的功能。

我正在使用RNFS库,特别是unlink函数。当函数完成时,没有错误抛出,但它不会从我的手机中删除文件。

为了分离问题,我创建了一个新的应用程序,使用了AwesomeProject模板,并添加了以下代码:

import RNFS from 'react-native-fs';
import { PermissionsAndroid } from 'react-native';

在App.tsx中。

const useEffectFunction = async () => {
  const permission = PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE;

  const hasPermission = await PermissionsAndroid.check(permission);
  if (!hasPermission) {
    await PermissionsAndroid.request(permission);
  }

  RNFS.unlink('/storage/emulated/0/Download/1.pdf')
    .then(() => {
      console.log('file deleted');
    })
    .catch((e: any) => console.log(e));
};

useEffect(() => {
  useEffectFunction();
}, []);

在App.tsx中的App组件内。

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_MEDIA_IMAGES"/>
<uses-permission android:name="android.permission.READ_MEDIA_VIDEO"/>

在AndroidManifest.xml中。

所有这些代码只是请求写入存储的权限,然后删除文件系统中的文件。

如果路径不存在,我会收到" [错误:文件不存在]"消息,

如果路径正确,我会收到"文件已删除"而没有错误,但是文件没有从我的设备中删除,我仍然可以在Android文件管理器中找到它。

英文:

I'm making a gallery style app where I need to add the functionality of deleting images.

I'm using the RNFS library, specifically the unlink function. When finished the function throws no errors but it does not delete the file from my phone.

To isolate the problem, I create a new app using the AwesomeProject template and added these lines of code :

import RNFS from &#39;react-native-fs&#39;;
import {PermissionsAndroid} from &#39;react-native&#39;;

In App.tsx.

  const usEffectFunction = async () =&gt; {
    const permission = PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE;

    const hasPermission = await PermissionsAndroid.check(permission);
    if (!hasPermission) {
      await PermissionsAndroid.request(permission);
    }

    RNFS.unlink(&#39;/storage/emulated/0/Download/1.pdf&#39;)
      .then(() =&gt; {
        console.log(&#39;file deleted&#39;);
      })
      .catch((e: any) =&gt; console.log(e));
  };

  useEffect(() =&gt; {
    usEffectFunction();
  }, []);

In App.tsx inside the App component.

&lt;uses-permission android:name=&quot;android.permission.READ_EXTERNAL_STORAGE&quot;/&gt;
&lt;uses-permission android:name=&quot;android.permission.WRITE_EXTERNAL_STORAGE&quot;/&gt;
&lt;uses-permission android:name=&quot;android.permission.READ_MEDIA_IMAGES&quot;/&gt;
&lt;uses-permission android:name=&quot;android.permission.READ_MEDIA_VIDEO&quot;/&gt;

In AndroidManifest.xml.

All this code does is ask for permission to write to storage and then delete a file in my file system.

If the path doesn't exist I get "[Error: File does not exist]" message,

If the path is correct I get "file deleted" and no error, but the file doesn't get deleted from my device, I can still find it using the Android file manager.

答案1

得分: 0

以下是翻译好的部分:

  • react-native-fs库不支持Android 10(API级别29)引入的新范围存储(scoped storage)在删除媒体文件时的使用。

  • 使用新的存储系统,您需要请求Android操作系统显示一个活动,以请求用户的权限来删除媒体文件,使用MediaStore.createDeleteRequest

  • 在我的研究中,我找不到任何使用MediaStore.createDeleteRequest来实现媒体删除的React Native模块。

我通过创建自己的npm包react-native-delete-media来解决了我的问题。

它是一个简单的模块,只有一个函数deletePhotos,它接受一个URI数组,并请求Android操作系统删除它。

英文:

After some research here is the information I learned.

  • The react-native-fs library does not support the new scoped storage introduced in Android 10 (API level 29) when deleting media.

  • With the new storage system, you need to ask the android OS to display an activity that asks the user's permission to delete the media file using MediaStore.createDeleteRequest.

  • In my research, I couldn't find any react-native module that uses the implements media deletion using MediaStore.createDeleteRequest.

I was able to solve my problem by creating my own npm package called react-native-delete-media.

It's a simple module that has a single function deletePhotos which takes an array of uris and asks the Android OS to delete it.

huangapple
  • 本文由 发表于 2023年3月7日 01:37:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/75654057.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定