gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 11:27:36 25 4
gpt4 key购买 nike

我正在寻找某人来协助我在我的应用程序中需要的代码,以将图像从存储在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com