Android如何将媒体图片移动到另一个文件夹?

发布于 2024-12-18 20:19:58 字数 1092 浏览 0 评论 0原文

在我的android应用程序中,我想将媒体图像复制到另一个文件夹(在下面的代码中,我尝试将图片从“/mnt/sdcard/DCIM/Camera/my_photo.jpg”复制到“/mnt/sdcard/PortFolio/MyGallery/” .我用下面的代码尝试了这个,但它不起作用?有人帮助我吗?还有其他方法吗?

File sd = Environment.getExternalStorageDirectory();
            File data = Environment.getDataDirectory();
            if (sd.canWrite()) {
                String sourceImagePath= "/mnt/sdcard/DCIM/Camera/my_photo.jpg";
                String destinationImagePath= "/mnt/sdcard/PortFolio/MyGallery/";
                Log.d("destinationImagePath", ""+destinationImagePath);
                File source= new File(data, sourceImagePath);
                File destination= new File(sd, destinationImagePath);
                Log.d("before copying", "");
                if (source.exists()) {
                    FileChannel src = new FileInputStream(source).getChannel();
                    FileChannel dst = new FileOutputStream(destination).getChannel();
                    dst.transferFrom(src, 0, src.size());
                    src.close();
                    dst.close();
                }

In my android application i want to copy the media images to another folder(in my below code i try to copy picture from "/mnt/sdcard/DCIM/Camera/my_photo.jpg" to "/mnt/sdcard/PortFolio/MyGallery/ . I tried this with the following code but it doesn't work. Someone help me out of this??? Is there any other way possible??

File sd = Environment.getExternalStorageDirectory();
            File data = Environment.getDataDirectory();
            if (sd.canWrite()) {
                String sourceImagePath= "/mnt/sdcard/DCIM/Camera/my_photo.jpg";
                String destinationImagePath= "/mnt/sdcard/PortFolio/MyGallery/";
                Log.d("destinationImagePath", ""+destinationImagePath);
                File source= new File(data, sourceImagePath);
                File destination= new File(sd, destinationImagePath);
                Log.d("before copying", "");
                if (source.exists()) {
                    FileChannel src = new FileInputStream(source).getChannel();
                    FileChannel dst = new FileOutputStream(destination).getChannel();
                    dst.transferFrom(src, 0, src.size());
                    src.close();
                    dst.close();
                }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

握住你手 2024-12-25 20:19:58

sd 已包含 /mnt/sdcard。您实际上正在尝试打开 /mnt/sdcard/mnt/sdcard/DCIM/Camera/my_photo.jpg。从 sourceImagePathdestinationImagePath 中删除 /mnt/sdcard。您可能还需要创建 PortFolio首先是 /MyGallery 文件夹。

从 API 级别 8 开始,您还可以使用它来获取默认图片文件夹:

Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);

最后但并非最不重要的一点是,请确保您 有权限访问SD卡。

sd already contains /mnt/sdcard. You are actually trying to open /mnt/sdcard/mnt/sdcard/DCIM/Camera/my_photo.jpg. Remove /mnt/sdcard from sourceImagePath and destinationImagePath. You would probably also need to create the PortFolio/MyGallery folder first.

Starting API level 8, you can also use this to get the default pictures folder:

Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);

And last but not least, make sure you have permission to access the SD card.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文