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

标签 android image copy sd-card

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

最佳答案

乌斯曼

您可以使用以下命令启动图库选择器 Intent :

    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) {}

关于Android 将图库文件夹中的图像复制到 SD 卡替代文件夹中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4921183/

相关文章:

android - 多个 fragment 同时访问同一个数据库

ios - 检测图像中的波纹图案-iOS Swift

php - 将 blob 图像(存储在数据库中)下载到我的计算机

Javascript 清理从 html 标签复制的数据

java - 将源的子文件复制到 Java 中的目标

c++ - 在复制构造函数中复制 vector 的 vector

Android ListView选择动画

android - activity_main.xml : String index out of range: 0 in Android Code

android通知点击不工作

matlab - MATLAB 中的用户定义函数