【问题标题】:Store an image from gallery to a different folder将图库中的图像存储到不同的文件夹
【发布时间】:2016-04-07 06:05:48
【问题描述】:

到目前为止,我所取得的成就是我可以将从相机点击的图像存储到新文件夹中

    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);

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

【问题讨论】:

  • 您正在创建项目名称的文件夹,您只需将捕获图像复制到您的项目名称文件夹中????要你先澄清一下..
  • 这个话题可以帮到你:stackoverflow.com/questions/11846108/…
  • Utils.getPath() 返回我要创建新文件夹来存储图像的位置。休息由意图处理。但是当您从图库中选择图像时,MediaStore.EXTRA_OUTPUT 不起作用。那么如何将从图库中选择的图像存储到另一个文件夹?

标签: android file android-gallery


【解决方案1】:

首先,从你从画廊获得的 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);

查看网址了解更多详情,

  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

【讨论】:

    【解决方案2】:

    上面的解决方案对我有用,但有细微的变化:

    相机点击的图片由 ACTION_IMAGE_CAPTURE Intent 保存。而对于我们自己的应用程序从图库中挑选图像,我们需要保存来自 ACTION_PICK 意图的图像。在这种情况下保存基本上是在 ImageView 中显示图像后将图像从图库复制到您自己的应用程序文件夹。

    首先你需要有目标文件:所以调用以下方法来获取你的应用程序的目录,它位于Android>data>YOUR APP FOLDER>Files>Pictures:

    private File mPhotoFile;
    mPhotoFile=PhotoLab.get(getActivity()).getPhotoFile(mPhoto);
    

    这里是 getPhotoFile() 函数

    public File getPhotoFile(Photo photo){
        File externalFilesDir=mContext.getExternalFilesDir(Environment.DIRECTORY_PICTURES);//getExternalStorageDirectory().toString());
    
        if(externalFilesDir==null){
            return null;
        }
        return  new File(externalFilesDir, photo.getPhotoFilename());
    }
    

    现在您有了要保存由 Image Intent 选取的图像的文件。现在调用 ACTION_PICK 意图。

    private static final int PICK_IMAGE_REQUEST=2;
    Intent pickIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);  //Trying intent to pick image from gallery
                        pickIntent.setType("image/*");
                        Uri uriImagePath = Uri.fromFile(mPhotoFile);
                        pickIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriImagePath);
                        startActivityForResult(pickIntent, PICK_IMAGE_REQUEST);
    

    在onActivityResult中收到结果后,下面是我的代码:

       if(requestCode==PICK_IMAGE_REQUEST){
            Picasso.with(getActivity()).load(data.getData()).fit().into(mPhotoView);
            File file=getBitmapFile(data);
    
    
            try {
               copyFile(file, mPhotoFile);
            }catch(IOException e){
              Toast.makeText(getActivity(),"Cannot use Gallery, Try Camera!",Toast.LENGTH_SHORT).show();
                e.printStackTrace();
            }
        }
    

    获取数据后,我将其传递给 next 方法,以通过 Intent.ACTION_PICK 找到所取图像的绝对路径。以下是方法定义:

    public File getBitmapFile(Intent data){
        Uri selectedImage=data.getData();
        Cursor cursor=getContext().getContentResolver().query(selectedImage, new String[]{MediaStore.Images.ImageColumns.DATA}, null, null, null);
        cursor.moveToFirst();
    
        int idx=cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
        String selectedImagePath=cursor.getString(idx);
        cursor.close();
    
        return new File(selectedImagePath);
    
    }
    

    获取到所选取图片的绝对路径后。我调用了 copyFile() 函数。如下。请记住,我已经生成了新的文件路径。

    public File getBitmapFile(Intent data){
        Uri selectedImage=data.getData();
        Cursor cursor=getContext().getContentResolver().query(selectedImage, new String[]{MediaStore.Images.ImageColumns.DATA}, null, null, null);
        cursor.moveToFirst();
    
        int idx=cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
        String selectedImagePath=cursor.getString(idx);
        cursor.close();
    
        return new File(selectedImagePath);
    
    }
    

    我的目的地是 mPhotoFile。我们首先生成的。 如果您想从图库中选择图像并将其存储以在每次打开时进一步显示在应用程序的 imageView 中,则整个代码将起作用。

    【讨论】:

    • 你的getBitmapFile函数返回null,答案是缺少copyFile函数
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-07-01
    • 2020-08-26
    • 2018-05-15
    • 2010-10-30
    • 2016-07-02
    • 1970-01-01
    • 2020-10-16
    相关资源
    最近更新 更多