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

发布于 2024-12-23 02:13:26 字数 579 浏览 3 评论 0原文

我想从图库中选择图像并将其复制到 SDCard 中的其他文件夹中。

从图库中选取图像的代码

Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
    photoPickerIntent.setType("image/*");
    startActivityForResult(photoPickerIntent, REQUEST_CODE_CHOOSE_PICTURE_FROM_GALLARY);

我在ActivityResult 上得到 content://media/external/images/media/681 这个 URI。

我想复制图像,

形式 path ="content://media/external/images/media/681

path = "file:///mnt/sdcard/sharedresources/< /code> Android 中 sdcard 的这个路径。

如何做到这一点?

I want to pick image from gallery and copy it in to other folder in SDCard.

Code to Pick Image from Gallery

Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
    photoPickerIntent.setType("image/*");
    startActivityForResult(photoPickerIntent, REQUEST_CODE_CHOOSE_PICTURE_FROM_GALLARY);

I get content://media/external/images/media/681 this URI onActivityResult.

I want to copy the image,

Form path ="content://media/external/images/media/681

To path = "file:///mnt/sdcard/sharedresources/ this path of sdcard in Android.

How to do this?

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

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

发布评论

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

评论(6

听风念你 2024-12-30 02:13:26

感谢大家...工作代码在这里..

     private OnClickListener photoAlbumListener = new OnClickListener(){
          @Override
          public void onClick(View arg0) {
            Intent photoPickerIntent = new Intent(Intent.ACTION_GET_CONTENT);
            imagepath = Environment.getExternalStorageDirectory()+"/sharedresources/"+HelperFunctions.getDateTimeForFileName()+".png";
            uriImagePath = Uri.fromFile(new File(imagepath));
            photoPickerIntent.setType("image/*");
            photoPickerIntent.putExtra(MediaStore.EXTRA_OUTPUT,uriImagePath);
            photoPickerIntent.putExtra("outputFormat",Bitmap.CompressFormat.PNG.name());
            photoPickerIntent.putExtra("return-data", true);
            startActivityForResult(photoPickerIntent, REQUEST_CODE_CHOOSE_PICTURE_FROM_GALLARY);

          }
      };

   protected void onActivityResult(int requestCode, int resultCode, Intent data) {
           if (resultCode == RESULT_OK) {
                switch(requestCode){
               
              
                 case 22:
                        Log.d("onActivityResult","uriImagePath Gallary :"+data.getData().toString());
                        Intent intentGallary = new Intent(mContext, ShareInfoActivity.class);
                        intentGallary.putExtra(IMAGE_DATA, uriImagePath);
                        intentGallary.putExtra(TYPE, "photo");
                        File f = new File(imagepath);
                        if (!f.exists())
                        {
                            try {
                                f.createNewFile();
                                copyFile(new File(getRealPathFromURI(data.getData())), f);
                            } catch (IOException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
                        }
                        
                        startActivity(intentGallary);
                        finish();
                 break;
                 
                 
                }
              }
           
           
        
          
        
   }

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

    private String getRealPathFromURI(Uri contentUri) {
    
       String[] proj = { MediaStore.Video.Media.DATA };
       Cursor cursor = managedQuery(contentUri, proj, null, null, null);
       int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
       cursor.moveToFirst();
       return cursor.getString(column_index);
    }

Thanks to all ... Working Code is Here..

     private OnClickListener photoAlbumListener = new OnClickListener(){
          @Override
          public void onClick(View arg0) {
            Intent photoPickerIntent = new Intent(Intent.ACTION_GET_CONTENT);
            imagepath = Environment.getExternalStorageDirectory()+"/sharedresources/"+HelperFunctions.getDateTimeForFileName()+".png";
            uriImagePath = Uri.fromFile(new File(imagepath));
            photoPickerIntent.setType("image/*");
            photoPickerIntent.putExtra(MediaStore.EXTRA_OUTPUT,uriImagePath);
            photoPickerIntent.putExtra("outputFormat",Bitmap.CompressFormat.PNG.name());
            photoPickerIntent.putExtra("return-data", true);
            startActivityForResult(photoPickerIntent, REQUEST_CODE_CHOOSE_PICTURE_FROM_GALLARY);

          }
      };

   protected void onActivityResult(int requestCode, int resultCode, Intent data) {
           if (resultCode == RESULT_OK) {
                switch(requestCode){
               
              
                 case 22:
                        Log.d("onActivityResult","uriImagePath Gallary :"+data.getData().toString());
                        Intent intentGallary = new Intent(mContext, ShareInfoActivity.class);
                        intentGallary.putExtra(IMAGE_DATA, uriImagePath);
                        intentGallary.putExtra(TYPE, "photo");
                        File f = new File(imagepath);
                        if (!f.exists())
                        {
                            try {
                                f.createNewFile();
                                copyFile(new File(getRealPathFromURI(data.getData())), f);
                            } catch (IOException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
                        }
                        
                        startActivity(intentGallary);
                        finish();
                 break;
                 
                 
                }
              }
           
           
        
          
        
   }

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

    private String getRealPathFromURI(Uri contentUri) {
    
       String[] proj = { MediaStore.Video.Media.DATA };
       Cursor cursor = managedQuery(contentUri, proj, null, null, null);
       int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
       cursor.moveToFirst();
       return cursor.getString(column_index);
    }
宫墨修音 2024-12-30 02:13:26
OutputStream out;
            String root = Environment.getExternalStorageDirectory().getAbsolutePath()+"/";
            File createDir = new File(root+"Folder Name"+File.separator);
            if(!createDir.exists()) {
                createDir.mkdir();
            }
            File file = new File(root + "Folder Name" + File.separator +"Name of File");
            file.createNewFile();
            out = new FileOutputStream(file);                       

        out.write(data);
        out.close();

希望它能帮助你

OutputStream out;
            String root = Environment.getExternalStorageDirectory().getAbsolutePath()+"/";
            File createDir = new File(root+"Folder Name"+File.separator);
            if(!createDir.exists()) {
                createDir.mkdir();
            }
            File file = new File(root + "Folder Name" + File.separator +"Name of File");
            file.createNewFile();
            out = new FileOutputStream(file);                       

        out.write(data);
        out.close();

Hope it will help u

小糖芽 2024-12-30 02:13:26

一种解决方案是,

1)从所选文件的 inputStream 读取字节。

<块引用>

我在ActivityResult 上得到“content://media/external/images/media/681”这个 URI。
你可以通过查询你得到的这个Uri来获取文件名。获取它的输入流。将其读入byte[]。

给你/

Uri u = Uri.Parse("content://media/external/images/media/681");

游标cursor = contentResolver.query(u, null, null, null, null);
有一个列名“_data”,它将返回您的文件名,从文件名您可以创建输入流,

您现在可以读取此输入流

         byte data=new byte[fis.available()];
          fis.read(data);

所以您有带有图像字节的数据(字节数组)

2)在SD卡上创建一个文件,并且使用第一步中获取的 byte[] 进行写入。

       File file=new File(fileOnSD.getAbsolutePath() +"your foldername", fileName);
        FileOutputStream fout=new FileOutputStream(file, false);
        fout.write(data);

作为您已从查询方法中获得的文件名,请在此处使用相同的文件名。

one solution can be,

1) read bytes from inputStream of the picked file.

i get "content://media/external/images/media/681" this URI onActivityResult.
You can get the file name by querying this Uri u got. get inputStream of it. read it into byte[].

here you go/

Uri u = Uri.Parse("content://media/external/images/media/681");

Cursor cursor = contentResolver.query(u, null, null, null, null);
there is a column name "_data" which will return you the filename, from filename you can create inputstream,

you can now read this input stream

         byte data=new byte[fis.available()];
          fis.read(data);

So you have data(byte array) with images byte

2) create a file on to sdcard, and write with byte[] taken in step one.

       File file=new File(fileOnSD.getAbsolutePath() +"your foldername", fileName);
        FileOutputStream fout=new FileOutputStream(file, false);
        fout.write(data);

as fileName you already have from the query method, use same here.

书间行客 2024-12-30 02:13:26

Was reading this link, here they are talking about four ways to copy files in Java,
so relevant for android as well.

Though author concludes that using 'channel' as used in @Prashant's answer are the best way, you may even explore other ways.

(I have tried first two, and both of them work find)

粉红×色少女 2024-12-30 02:13:26

尽管我已经赞成@AAnkit的答案,但我借用并继续修改了一些项目。他提到使用Cursor,但如果没有正确的说明,新手可能会感到困惑。

我认为这比得票最多的答案更简单。

String mCurrentPhotoPath = "";


private File createImageFile() throws IOException {
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    String imageFileName = "JPEG_" + timeStamp + "_";
    File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
    File image = File.createTempFile(
            imageFileName,  /* prefix */
            ".jpg",         /* suffix */
            storageDir      /* directory */
    );

    mCurrentPhotoPath = image.getAbsolutePath();
    return image;
}


                   /*Then I proceed to select from gallery and when its done selecting it calls back the onActivityResult where I do some magic*/


private void snapOrSelectPicture() {
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
        File photoFile = null;
        try {
            photoFile = createImageFile();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        if (photoFile != null) {
            Uri photoURI = FileProvider.getUriForFile(this,
                    "com.example.android.fileprovider",
                    photoFile);
            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
            startActivityForResult(Intent.createChooser(takePictureIntent, "SELECT FILE"), 1001);
        }
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK) {

        try {
            /*data.getDataString() contains your path="content://media/external/images/media/681 */

            Uri u = Uri.parse(data.getDataString());
            Cursor cursor = getContentResolver().query(u, null, null, null, null);
            cursor.moveToFirst();
            File doc = new File(cursor.getString(cursor.getColumnIndex("_data")));
            File dnote = new File(mCurrentPhotoPath);
            FileOutputStream fout = new FileOutputStream(dnote, false);
            fout.write(Files.toByteArray(doc));
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}

Even though I have upvoted the answer by @AAnkit, I borrowed and went ahead to modify some items. He mentions to use Cursor but without proper illustration it can be confusing to newbies.

I think this is simpler than the most voted answer.

String mCurrentPhotoPath = "";


private File createImageFile() throws IOException {
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    String imageFileName = "JPEG_" + timeStamp + "_";
    File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
    File image = File.createTempFile(
            imageFileName,  /* prefix */
            ".jpg",         /* suffix */
            storageDir      /* directory */
    );

    mCurrentPhotoPath = image.getAbsolutePath();
    return image;
}


                   /*Then I proceed to select from gallery and when its done selecting it calls back the onActivityResult where I do some magic*/


private void snapOrSelectPicture() {
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
        File photoFile = null;
        try {
            photoFile = createImageFile();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        if (photoFile != null) {
            Uri photoURI = FileProvider.getUriForFile(this,
                    "com.example.android.fileprovider",
                    photoFile);
            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
            startActivityForResult(Intent.createChooser(takePictureIntent, "SELECT FILE"), 1001);
        }
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK) {

        try {
            /*data.getDataString() contains your path="content://media/external/images/media/681 */

            Uri u = Uri.parse(data.getDataString());
            Cursor cursor = getContentResolver().query(u, null, null, null, null);
            cursor.moveToFirst();
            File doc = new File(cursor.getString(cursor.getColumnIndex("_data")));
            File dnote = new File(mCurrentPhotoPath);
            FileOutputStream fout = new FileOutputStream(dnote, false);
            fout.write(Files.toByteArray(doc));
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
另类 2024-12-30 02:13:26

您可以使用此函数中的内容解析器将图像文件复制到任何其他文件夹

fun copyImageFileToMediaStore(
    context: Context,
    sourceFilePath: String,
    destinationFolder: File
) {
    val sourceFile = File(sourceFilePath)
    val extension = MimeTypeMap.getFileExtensionFromUrl(sourceFilePath)
    val mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension)
    val fileName = sourceFile.name
    val inputStream = FileInputStream(sourceFile)
    val resolver: ContentResolver = context.contentResolver

    // Check if a file with the same name already exists in the destination folder
    val projection = arrayOf(MediaStore.MediaColumns._ID)
    val selection =
        "${MediaStore.MediaColumns.DISPLAY_NAME} = ? AND ${MediaStore.MediaColumns.RELATIVE_PATH} = ? AND ${MediaStore.MediaColumns.DATA} = ?"
    val selectionArgs = arrayOf(
        fileName,
        "${destinationFolder.path}/",
        destinationFolder.absolutePath + File.separator + fileName
    )
    val cursor = resolver.query(
        MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
        projection,
        selection,
        selectionArgs,
        null
    )
    if (cursor != null && cursor.count > 0) {
        // File already exists in the MediaStore, do not insert it again
        cursor.close()
        return
    }

    // File does not exist in the MediaStore, insert it
    val values = ContentValues().apply {
        put(MediaStore.MediaColumns.DISPLAY_NAME, fileName)
        put(MediaStore.MediaColumns.MIME_TYPE, mimeType)
        put(MediaStore.MediaColumns.RELATIVE_PATH, destinationFolder.parent)
        put(
            MediaStore.MediaColumns.DATA,
            destinationFolder.absolutePath + File.separator + fileName
        )
    }
    var outputStream: FileOutputStream? = null
    var uri: Uri? = null
    try {
        val contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI
        uri = resolver.insert(contentUri, values)
        if (uri == null) {
            throw IOException("Failed to create new MediaStore record.")
        }
        outputStream = resolver.openOutputStream(uri) as FileOutputStream?
        if (outputStream == null) {
            throw IOException("Failed to get output stream.")
        }
        val buffer = ByteArray(4 * 1024)
        var read: Int
        while (inputStream.read(buffer).also { read = it } != -1) {
            outputStream.write(buffer, 0, read)
        }
        outputStream.flush()
    } catch (e: IOException) {
        uri?.let { resolver.delete(it, null, null) }
        uri = null
    } finally {
        inputStream.close()
        outputStream?.close()
    }
}

目录 像这样调用此函数

    val destinationFolder = File("/storage/emulted/0/DCIM/foldername")
    val sourceFilePath = "/storage/emulted/0/DCIM/anyOtherFolder/abcd.jpeg"
    copyImageFileToMediaStore(requireContext(), sourceFilePath, destinationFolder)

You can copy image files to any other folder directory using Content Resolver from this function

fun copyImageFileToMediaStore(
    context: Context,
    sourceFilePath: String,
    destinationFolder: File
) {
    val sourceFile = File(sourceFilePath)
    val extension = MimeTypeMap.getFileExtensionFromUrl(sourceFilePath)
    val mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension)
    val fileName = sourceFile.name
    val inputStream = FileInputStream(sourceFile)
    val resolver: ContentResolver = context.contentResolver

    // Check if a file with the same name already exists in the destination folder
    val projection = arrayOf(MediaStore.MediaColumns._ID)
    val selection =
        "${MediaStore.MediaColumns.DISPLAY_NAME} = ? AND ${MediaStore.MediaColumns.RELATIVE_PATH} = ? AND ${MediaStore.MediaColumns.DATA} = ?"
    val selectionArgs = arrayOf(
        fileName,
        "${destinationFolder.path}/",
        destinationFolder.absolutePath + File.separator + fileName
    )
    val cursor = resolver.query(
        MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
        projection,
        selection,
        selectionArgs,
        null
    )
    if (cursor != null && cursor.count > 0) {
        // File already exists in the MediaStore, do not insert it again
        cursor.close()
        return
    }

    // File does not exist in the MediaStore, insert it
    val values = ContentValues().apply {
        put(MediaStore.MediaColumns.DISPLAY_NAME, fileName)
        put(MediaStore.MediaColumns.MIME_TYPE, mimeType)
        put(MediaStore.MediaColumns.RELATIVE_PATH, destinationFolder.parent)
        put(
            MediaStore.MediaColumns.DATA,
            destinationFolder.absolutePath + File.separator + fileName
        )
    }
    var outputStream: FileOutputStream? = null
    var uri: Uri? = null
    try {
        val contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI
        uri = resolver.insert(contentUri, values)
        if (uri == null) {
            throw IOException("Failed to create new MediaStore record.")
        }
        outputStream = resolver.openOutputStream(uri) as FileOutputStream?
        if (outputStream == null) {
            throw IOException("Failed to get output stream.")
        }
        val buffer = ByteArray(4 * 1024)
        var read: Int
        while (inputStream.read(buffer).also { read = it } != -1) {
            outputStream.write(buffer, 0, read)
        }
        outputStream.flush()
    } catch (e: IOException) {
        uri?.let { resolver.delete(it, null, null) }
        uri = null
    } finally {
        inputStream.close()
        outputStream?.close()
    }
}

Call this function like this

    val destinationFolder = File("/storage/emulted/0/DCIM/foldername")
    val sourceFilePath = "/storage/emulted/0/DCIM/anyOtherFolder/abcd.jpeg"
    copyImageFileToMediaStore(requireContext(), sourceFilePath, destinationFolder)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文