gpt4 book ai didi

android - 如何在 Android 中以编程方式将图像文件从图库复制到另一个文件夹

转载 作者:IT王子 更新时间:2023-10-28 23:28:14 27 4
gpt4 key购买 nike

我想从图库中挑选图像并将其复制到 SDCard 的其他文件夹中。

从图库中选择图片的代码

Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, REQUEST_CODE_CHOOSE_PICTURE_FROM_GALLARY);

我得到 content://media/external/images/media/681 这个 URI onActivityResult。

我想复制图片,

表单path="content://media/external/images/media/681

path = "file:///mnt/sdcard/sharedresources/这个sdcard在Android中的路径。

如何做到这一点?

最佳答案

感谢大家...工作代码在这里..

     private OnClickListener photoAlbumListener = new OnClickListener(){
@Override
public void onClick(View arg0) {
Intent photoPickerIntent = new Intent(Intent.ACTION_GET_CONTENT);
imagepath = Environment.getExternalStorageDirectory()+"/sharedresources/"+HelperFunctions.getDateTimeForFileName()+".png";
uriImagePath = Uri.fromFile(new File(imagepath));
photoPickerIntent.setType("image/*");
photoPickerIntent.putExtra(MediaStore.EXTRA_OUTPUT,uriImagePath);
photoPickerIntent.putExtra("outputFormat",Bitmap.CompressFormat.PNG.name());
photoPickerIntent.putExtra("return-data", true);
startActivityForResult(photoPickerIntent, REQUEST_CODE_CHOOSE_PICTURE_FROM_GALLARY);

}
};

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
switch(requestCode){


case 22:
Log.d("onActivityResult","uriImagePath Gallary :"+data.getData().toString());
Intent intentGallary = new Intent(mContext, ShareInfoActivity.class);
intentGallary.putExtra(IMAGE_DATA, uriImagePath);
intentGallary.putExtra(TYPE, "photo");
File f = new File(imagepath);
if (!f.exists())
{
try {
f.createNewFile();
copyFile(new File(getRealPathFromURI(data.getData())), f);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

startActivity(intentGallary);
finish();
break;


}
}





}

private void copyFile(File sourceFile, File destFile) throws IOException {
if (!sourceFile.exists()) {
return;
}

FileChannel source = null;
FileChannel destination = null;
source = new FileInputStream(sourceFile).getChannel();
destination = new FileOutputStream(destFile).getChannel();
if (destination != null && source != null) {
destination.transferFrom(source, 0, source.size());
}
if (source != null) {
source.close();
}
if (destination != null) {
destination.close();
}


}

private String getRealPathFromURI(Uri contentUri) {

String[] proj = { MediaStore.Video.Media.DATA };
Cursor cursor = managedQuery(contentUri, proj, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}

关于android - 如何在 Android 中以编程方式将图像文件从图库复制到另一个文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8664440/

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