android复制图片到sd卡上,Android将图库文件夹中的图像复制到SD卡替代文件夹中

Usmaan,

您可以使用以下命令启动图库选取器意图:

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值