【问题标题】:Android copy image from gallery folder onto SD Card alternative folderAndroid将图像从图库文件夹复制到SD卡替代文件夹
【发布时间】:2011-06-22 17:07:21
【问题描述】:

我正在寻找某人来协助我在我的应用程序中所需的代码,以将图像从它们存储在 HTC 愿望作为标准(图库)的位置复制到 SD 卡上的另一个文件夹。我希望用户能够单击一个按钮并将某个文件从 SD 卡库文件夹复制到 SD 卡上的另一个文件夹?谢谢

【问题讨论】:

    标签: android image copy sd-card


    【解决方案1】:

    乌斯曼,

    您可以通过以下方式启动图库选择器意图:

        public void imageFromGallery() {
        Intent getImageFromGalleryIntent = 
          new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI);
        startActivityForResult(getImageFromGalleryIntent, SELECT_IMAGE);
    }
    

    返回时,通过以下部分代码获取选中图片的路径:

    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch(requestCode) {
            case SELECT_IMAGE:
                mSelectedImagePath = getPath(data.getData());
                break;
        }
    }
    
    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);
    }
    

    现在您已将路径名包含在字符串中,您可以将其复制到另一个位置。

    干杯!

    编辑:如果您只需要复制文件,请尝试...

    try {
        File sd = Environment.getExternalStorageDirectory();
        File data = Environment.getDataDirectory();
        if (sd.canWrite()) {
            String sourceImagePath= "/path/to/source/file.jpg";
            String destinationImagePath= "/path/to/destination/file.jpg";
            File source= new File(data, sourceImagePath);
            File destination= new File(sd, destinationImagePath);
            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();
            }
        }
    } catch (Exception e) {}
    

    【讨论】:

    • 对不起,我不明白,我知道在我的应用程序中拍照等很热......但稍后我想将图像从画廊文件夹复制到另一个文件夹,我不确定这段代码是这样做的吗?
    • 此代码的第一部分将启动您的图库并允许您选择图像。选择图像后,它将从我发布的代码的第二部分调用onActivityResult()。它将获取从图库应用程序返回的数据,我发布的 getPath() 函数会将所选图像的完整路径作为字符串返回给您。此代码不允许您使用相机拍照。
    • 我想将图像复制到不同的文件夹..另外我真的不想打开图库,我已经知道图像名称并且我知道它存储在 sd 卡上 sdcard/dcim/图片名称
    • 寻找类似将 sdcard/dcim/image.jpg 复制到 sdcard/newfolder 的内容
    • 哦...您只需要实际的副本吗?哈哈对不起,我发布了导致那件事的所有内容,对不起>
    【解决方案2】:

    图库图像已存储在 Android 手机的 SD 卡中。官方文档在working with external storage 上有一个很好的部分,你应该看看。

    【讨论】:

    • 对不起,我的意思是说将图像从该文件夹复制到另一个
    猜你喜欢
    • 1970-01-01
    • 2011-08-08
    • 1970-01-01
    • 2011-08-06
    • 2014-02-10
    • 1970-01-01
    • 1970-01-01
    • 2012-06-20
    • 2013-11-22
    相关资源
    最近更新 更多