如何在 Android 中以编程方式将图像文件从图库复制到另一个文件夹
我想从图库中选择图像并将其复制到 SDCard 中的其他文件夹中。
从图库中选取图像的代码
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, REQUEST_CODE_CHOOSE_PICTURE_FROM_GALLARY);
我在ActivityResult 上得到 content://media/external/images/media/681
这个 URI。
我想复制图像,
形式 path ="content://media/external/images/media/681
到 path = "file:///mnt/sdcard/sharedresources/< /code> Android 中 sdcard 的这个路径。
如何做到这一点?
I want to pick image from gallery and copy it in to other folder in SDCard.
Code to Pick Image from Gallery
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, REQUEST_CODE_CHOOSE_PICTURE_FROM_GALLARY);
I get content://media/external/images/media/681
this URI onActivityResult.
I want to copy the image,
Form path ="content://media/external/images/media/681
To path = "file:///mnt/sdcard/sharedresources/
this path of sdcard in Android.
How to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
感谢大家...工作代码在这里..
Thanks to all ... Working Code is Here..
希望它能帮助你
Hope it will help u
一种解决方案是,
1)从所选文件的 inputStream 读取字节。
给你/
Uri u = Uri.Parse("content://media/external/images/media/681");
游标cursor = contentResolver.query(u, null, null, null, null);
有一个列名“_data”,它将返回您的文件名,从文件名您可以创建输入流,
您现在可以读取此输入流
所以您有带有图像字节的数据(字节数组)
2)在SD卡上创建一个文件,并且使用第一步中获取的 byte[] 进行写入。
作为您已从查询方法中获得的文件名,请在此处使用相同的文件名。
one solution can be,
1) read bytes from inputStream of the picked file.
here you go/
Uri u = Uri.Parse("content://media/external/images/media/681");
Cursor cursor = contentResolver.query(u, null, null, null, null);
there is a column name "_data" which will return you the filename, from filename you can create inputstream,
you can now read this input stream
So you have data(byte array) with images byte
2) create a file on to sdcard, and write with byte[] taken in step one.
as fileName you already have from the query method, use same here.
正在阅读此链接,这里他们讲的是Java中复制文件的四种方式,
与 Android 也相关。
尽管作者得出结论,使用 @Prashant 的答案中使用的“通道”是最好的方法,但您甚至可以探索其他方法。
(我已经尝试过前两个,并且都可以找到)
Was reading this link, here they are talking about four ways to copy files in Java,
so relevant for android as well.
Though author concludes that using 'channel' as used in @Prashant's answer are the best way, you may even explore other ways.
(I have tried first two, and both of them work find)
尽管我已经赞成@AAnkit的答案,但我借用并继续修改了一些项目。他提到使用
Cursor
,但如果没有正确的说明,新手可能会感到困惑。我认为这比得票最多的答案更简单。
Even though I have upvoted the answer by @AAnkit, I borrowed and went ahead to modify some items. He mentions to use
Cursor
but without proper illustration it can be confusing to newbies.I think this is simpler than the most voted answer.
您可以使用此函数中的内容解析器将图像文件复制到任何其他文件夹
目录 像这样调用此函数
You can copy image files to any other folder directory using Content Resolver from this function
Call this function like this