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

Ame*_*yaG 3 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);
Run Code Online (Sandbox Code Playgroud)

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

小智 6

首先,从您从画廊获得的 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);
}
Run Code Online (Sandbox Code Playgroud)

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

 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);
Run Code Online (Sandbox Code Playgroud)

查看网址了解更多详情,

  1. 如何在Android中以编程方式将图像文件从图库复制到另一个文件夹

  2. Android 将图库文件夹中的图像复制到 SD 卡替代文件夹


归档时间:

查看次数:

9297 次

最近记录:

7 年,10 月 前