Android - Choose Image from Gallery and store it in a File type variable

核能气质少年 提交于 2019-12-01 11:17:36

All you need to do is just create a File variable with the path of image which you've selected from gallery. Change your OnActivityResult as :

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == 1 && resultCode == RESULT_OK) {               
    Uri selectedImageUri = data.getData();
    imagepath = getPath(selectedImageUri); 
    File imageFile = new File(imagepath);
    }
}

Try this

You can try this also

public void onActivityResult(int requestCode, int resultCode, Intent data)
{

    if (requestCode == 1 && resultCode == RESULT_OK && data != null)
    {
        File destination = new File(Environment.getExternalStorageDirectory(),System.currentTimeMillis() + ".jpg");
 //You will get file path of captured camera image
        Bitmap photo = (Bitmap) data.getExtras().get("data");
        iv_profile.setImageBitmap(photo);
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!