【发布时间】:2011-06-22 17:07:21
【问题描述】:
我正在寻找某人来协助我在我的应用程序中所需的代码,以将图像从它们存储在 HTC 愿望作为标准(图库)的位置复制到 SD 卡上的另一个文件夹。我希望用户能够单击一个按钮并将某个文件从 SD 卡库文件夹复制到 SD 卡上的另一个文件夹?谢谢
【问题讨论】:
标签: android image copy sd-card
我正在寻找某人来协助我在我的应用程序中所需的代码,以将图像从它们存储在 HTC 愿望作为标准(图库)的位置复制到 SD 卡上的另一个文件夹。我希望用户能够单击一个按钮并将某个文件从 SD 卡库文件夹复制到 SD 卡上的另一个文件夹?谢谢
【问题讨论】:
标签: android image copy sd-card
乌斯曼,
您可以通过以下方式启动图库选择器意图:
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() 函数会将所选图像的完整路径作为字符串返回给您。此代码不允许您使用相机拍照。
图库图像已存储在 Android 手机的 SD 卡中。官方文档在working with external storage 上有一个很好的部分,你应该看看。
【讨论】: