移动/重命名SD卡文件 [英] Move/Rename file in SD Card

查看:539
本文介绍了移动/重命名SD卡文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从一个目录文件移动到另一个(SD卡)

我有一个文件的URI的方式和我试图移动:

 乌里selectedImage = imageReturnedIntent.getData(); //这个URI,类似的内容://媒体/外部/图像/媒体/ 635文件SD卡= Environment.getExternalStorageDirectory();
从=新的文件文件(SD卡,selectedImage);
文件以=新的文件(SD卡,myNewDir / mynewfile.jpg);
from.renameTo(到);

但它不工作,它也没有给我任何错误的logcat ...

编辑:

我已经加入这两个权限,以我的清单文件:

 <使用许可权的android:NAME =android.permission.WRITE_EXTERNAL_STORAG​​E/>
   <使用许可权的android:NAME =android.permission.READ_EXTERNAL_STORAG​​E/>


解决方案

  //这个URI,类似的内容://媒体/外部/图像/媒体/ 635

那么你在做什么,正在尝试来连接到这个 Environment.getExternalStorageDirectory()。这是行不通的。 内容://媒体/外部/图像/媒体/ 635 既不是一个相对的文件系统路径也没有一个绝对的文件系统路径。这是一个乌里

如果您希望将图像从乌里本地文件,使用 ContentResolver的来得到一个复制的InputStream 乌里 psented图像重新$ p $,然后使用Java I / O从复制字节在的InputStream 到目标文件。

I'm trying to move a file from one directory to another (in SD Card)

I have a file's URI and the way I'm trying to move it:

Uri selectedImage = imageReturnedIntent.getData(); // this the uri, something like content://media/external/images/media/635

File sdcard = Environment.getExternalStorageDirectory();
File from = new File(sdcard, selectedImage);
File to = new File(sdcard, "myNewDir/mynewfile.jpg");
from.renameTo(to);

But it doesnt work, neither does it give me any error in Logcat...

Edit:

I have added both permissions to my manifest file:

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

解决方案

// this the uri, something like content://media/external/images/media/635

What you are then doing is trying to concatenate this onto Environment.getExternalStorageDirectory(). This will not work. content://media/external/images/media/635 is neither a relative filesystem path nor an absolute filesystem path. It is a Uri.

If you wish to copy the image from the Uri to a local file, use a ContentResolver to get an InputStream on the image represented by the Uri, then use Java I/O to copy the bytes from the InputStream to your target file.

这篇关于移动/重命名SD卡文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆