当前位置:   article > 正文

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

android 12 从图库 拷贝文件

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/381102
推荐阅读
相关标签
  

闽ICP备14008679号