android - 将图库中的图像存储到不同的文件夹

标签 android file android-gallery

到目前为止我已经实现的是我可以将从相机点击的图像存储到一个新文件夹

    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        f = new File(Utils.getPath(), new Date().getTime() + ".jpg");
        intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
        startActivityForResult(intent, TAKE_PHOTO);

但我不知道如何将从图库中选择的图像存储到我创建的同一文件夹中。请帮我。 提前谢谢你。

最佳答案

首先,从您从画廊获得的 URI 获取真实路径。

public String getPath(Uri uri) {
    String[] projection = { MediaStore.Images.Media.DATA };
    Cursor cursor = managedQuery(uri, projection, null, null, null);
    startManagingCursor(cursor);
    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    return cursor.getString(column_index);
}

现在将图像复制到另一个位置,

 private void copyFile(File sourceFile, File destFile) throws IOException {
            if (!sourceFile.exists()) {
                return;
            }

            FileChannel source = null;
                FileChannel destination = null;
                source = new FileInputStream(sourceFile).getChannel();
                destination = new FileOutputStream(destFile).getChannel();
                if (destination != null && source != null) {
                    destination.transferFrom(source, 0, source.size());
                }
                if (source != null) {
                    source.close();
                }
                if (destination != null) {
                    destination.close();
                }  
    }



  File destFile = new File("Dest path");

  copyFile(new File(getPath(data.getData())), destFile);

查看 url 以获取更多详细信息,

  1. How to Copy Image File from Gallery to another folder programmatically in Android

  2. Android copy image from gallery folder onto SD Card alternative folder

关于android - 将图库中的图像存储到不同的文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36467907/

相关文章:

Android JNI java.lang.UnsatisfiedLinkError

java - 从数据库表中的 URL 路径将图像加载到 imageView

java - JSON 到 CSV 错误 - java.lang.NullPointerException

ruby - 使用 Ruby 逐行读取、编辑和写入文本文件

android - 如何修改此函数以返回图像路径?

android - NDK 无法在 NDK_MODULE_PATH 中找到我的模块 - android-ndk-profiler

android - 使用 SharedPreferences 的缺点?

perl - 为什么我不能在 Perl 中对数组使用菱形运算符?

android - 如何通过读取存储在 android 内部存储器中的所有图像来创建自定义图库

Android : Why Intent. EXTRA_LOCAL_ONLY 显示 Google 相册