我试图从一个目录文件移动到另一个(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_STORAGE/> <使用许可权的android:NAME =android.permission.READ_EXTERNAL_STORAGE/>
解决方案
//这个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.